正規表現 (JavaScript)
[履歴] (2013/08/02 18:37:07)
最近の投稿
注目の記事

概要

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.
関連ページ
    概要 AWS Lambda はイベントドリブンな「関数」を登録できるサービスです。例えば S3 に画像がアップロードされたときにサムネイル用のサイズに加工する処理が記述された関数を登録できます。基本的な使い方をまとめます。 事前準備 関数の登録はブラウザで AWS コンソールにログインして行うこともできますが、本ページでは