Reputation: 11
<script type="text/javascript">
var str="Jestem bardzo, bardzo zadowolony. Można powiedzieć, że jestem również uszczęśliwiony i uspokojony.";
patt1=new RegExp( "\bi\b", "g"); //<--- (to find the single word: "i")
document.write(str.match(patt1));
</script>
It works well as var pattern = /\bi\b/g;
but not when using RegExp("\bi\b","g")
. Why? (...thank you in advance)
Upvotes: 1
Views: 122
Reputation: 159618
\ is the escape character in JavaScript strings. It's also the escape character in regular expressions! Since you're passing a string to the RegExp constructor, you have to escape the escape character...
patt1=new RegExp( "\\bi\\b", "g");
Upvotes: 5