正規表現例
12196 ワード
let myStr = "Hello World!"
let myRegex = /Hello/
myRegex.test(myStr) // true
let myRegex2 = /hello/
myRegex2.test(myStr) // false
let myRegex3 = /hello/i
myRegex3.test(myStr) // true
myStr.match(myRegex) // "Hello"
let myStr2 = "Hello World! hello"
let myRegex4 = /Hello/ig
myStr2.match(myRegex4) // ["Hello,"hello"]
ワイルドカード期間とのマッチ何か
let humStr = "I'll hum a song";
let hugStr = "Bear hug";
let huRegex = /hu./;
huRegex.test(humStr); // true
huRegex.test(hugStr); // true
複数の可能性と一致する単一の文字
let bigStr = "big";
let bagStr = "bag";
let bugStr = "bug";
let bogStr = "bog";
let bgRegex = /b[aiu]g/;
bigStr.match(bgRegex); // ["big"]
bagStr.match(bgRegex); // ["bag"]
bugStr.match(bgRegex); // ["buig"]
bogStr.match(bgRegex); // null
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
let vowelRegex = /[aeiou]/ig;
let result = quoteSample.match(vowelRegex);
// [ 'e', 'a', 'e', 'o', 'u', 'i', 'e', 'a', 'o', 'e', 'o', 'e', 'I', 'a', 'e', 'o', 'o', 'e', 'i', 'o', 'e', 'o', 'i', 'e', 'i' ]
アルファベットマッチ文字
let catStr = "cat";
let batStr = "bat";
let matStr = "mat";
let bgRegex = /[a-e]at/;
catStr.match(bgRegex); //["cat"]
batStr.match(bgRegex); //["bat"]
matStr.match(bgRegex); //null
let quoteSample = "The quick brown fox jumps over the lazy dog.";
let alphabetRegex = /[a-z]/ig;
let result = quoteSample.match(alphabetRegex);//[ 'T', 'h', 'e', ... ,'d', 'o', 'g' ]
マッチ数とアルファベットの文字
let jennyStr = "Jenny8675309";
let myRegex = /[a-z0-9]/ig;
jennyStr.match(myRegex);
let quoteSample = "Blueberry 3.141592653s are delicious.";
let myRegex = /[h-s2-6]/ig;
let result = quoteSample.match(myRegex);
マッチしない単一の文字
let quoteSample = "3 blind mice.";
let myRegex = /[^aeiou0-9]/ig;
let result = quoteSample.match(myRegex);
1回以上発生する文字
let difficultSpelling = "Mississippi";
let myRegex = /s+/g;
let result = difficultSpelling.match(myRegex);
//[ 'ss', 'ss' ]
0回以上発生する文字
let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!"
let chewieRegex = /Aa*/;
let result = chewieQuote.match(chewieRegex); // ['Aaaaaaaaaaaaaaaa']
Reference
この問題について(正規表現例), 我々は、より多くの情報をここで見つけました https://dev.to/hambalee/regular-expression-example-4pp3テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol