jsrosetta

Standard library

Regex

Node.js's RegExp literals compared to Go's regexp, Rust's regex, Swift's Regex literals, and Java's java.util.regex: replace, test, and find all matches.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.65Swift ≥ 5.7Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

Node.js writes regexes as /pattern/flags literals right in the code. Go has no literal syntax for regexes — you compile a pattern into a *regexp.Regexp with regexp.MustCompile, and a flag like case-insensitivity lives inside the pattern string itself ((?i)) instead of outside like /i. Rust reaches for the external regex crate, with pattern syntax almost identical to Go (same RE2 family). Swift has a built-in Regex type since 5.7 with /pattern/ literals right in the language — closest to JS. Java uses the long-standing java.util.regex.Pattern/Matcher, with flags passed as Pattern.compile's second argument.

Replace, test, and find all matches

let input = 'foobar'
let replaced = input.replace(/foo(.*)/i, 'qux$1')
console.log(replaced) // quxbar
 
let match = /o{2}/i.test(input)
console.log(match) // true
 
input = '111-222-333'
let matches = input.match(/([0-9]+)/gi)
console.log(matches) // [ '111', '222', '333' ]

Reference: github.com/miguelmota/golang-for-nodejs-developers#regex