2016-11-24 15 views
0

単語全体をどのように一致させるのですか?regex return単語全体の一致結果

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<p>Click the button to do a global search for "is" in a strisng.</p> 
 

 
<button onclick="myFunction()">Try it</button> 
 

 
<p id="demo"></p> 
 

 
<script> 
 
function myFunction() { 
 
    var str = "Is Alvin this all there is sis?"; 
 
    var patt1 = /is|alvin/gi; 
 
    var result = str.match(patt1); 
 
    document.getElementById("demo").innerHTML = result; 
 
} 
 
</script> 
 

 
</body> 
 
</html>

+0

? – Mritunjay

+0

あなたが期待した結果がはっきりと現れました。 –

答えて

1

\b単語境界パターンを使用してください。期待されているもの

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<p>Click the button to do a global search for "is" in a strisng.</p> 
 

 
<button onclick="myFunction()">Try it</button> 
 

 
<p id="demo"></p> 
 

 
<script> 
 
function myFunction() { 
 
    var str = "Is Alvin this all there is sis?"; 
 
    var patt1 = /\b(?:is|alvin)\b/gi; 
 
    var result = str.match(patt1); 
 
    document.getElementById("demo").innerHTML = result; 
 
} 
 
</script> 
 

 
</body> 
 
</html>

関連する問題