find bitcoin addresses in a string using regex in javascript

To find bitcoin addresses in a string using regex in JavaScript, you can use the following regular expression pattern:

index.tsx
/([13][a-km-zA-HJ-NP-Z1-9]{25,34})/g
37 chars
2 lines

This pattern matches any string of characters that starts with either "1" or "3", followed by 25-34 letters and numbers (excluding "0", "O", "I", and "l"), which is the pattern for a typical bitcoin address.

Here's an example of how you can use this regex to find bitcoin addresses in a string:

index.tsx
const str = "Lorem ipsum dolor sit 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 amet, consectetur 3J98t1WpEZ73CNmQviecrnyiWrnqRh"; // example string
const regex = /([13][a-km-zA-HJ-NP-Z1-9]{25,34})/g; // regex pattern
const matches = str.match(regex); // array of matches

console.log(matches); // ['1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2', '3J98t1WpEZ73CNmQviecrnyiWrnqRh']
361 chars
6 lines

In this example, the match() function returns an array of all the matched strings in the given string. The output will contain all bitcoin addresses present in the string.

gistlibby LogSnag