JavaScriptの正規表現は、Perlのそれとほぼ同じです。
sample.js
var str = "This is a string.";
var arr = str.match(/is/g);
console.log(arr);
実行例
$ node sample.js
[ 'is', 'is' ]
sample.js
var pattern = /is/g;
// var pattern = new RegExp("is", 'g'); // 同じ意味
var str = "This is a string.";
var arr = str.match(pattern);
console.log(arr);
実行例
$ node sample.js
[ 'is', 'is' ]
sample.js
var str = "This is a string.";
console.log(str.search(/is/));
実行例
$ node sample.js
2
sample.js
var str = "This is a string.";
var replaced = str.replace(/is/g, "**");
console.log(str);
console.log(replaced);
実行例
$ node sample.js
This is a string.
Th** ** a string.
sample.js
var str = "This is a string.";
str = str.replace(/(is)/g, "[$1]");
console.log(str);
実行例
$ node sample.js
Th[is] [is] a string.