JavaScript / ECMAScript

Bitcoin Address in JS

Match Bitcoin addresses in legacy (P2PKH/P2SH) and SegWit Bech32 (bc1) formats.

Try it in the JS tester →

Pattern

regexJS
\b(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-zA-HJ-NP-Z0-9]{25,62})\b   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\b(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-zA-HJ-NP-Z0-9]{25,62})\\b", "g");
const input = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

Legacy addresses start with 1 or 3 followed by 25–34 Base58 characters (no 0, O, I, l). Bech32 SegWit addresses start with bc1 followed by 25–62 chars from its restricted alphabet.

Examples

Input

1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

Matches

  • 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

Input

3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy

Matches

  • 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy

Same pattern, other engines

← Back to Bitcoin Address overview (all engines)