RegExp \b Metacharacter - Javascript RegExp

Javascript examples for RegExp:Metacharacter

Description

The \b metacharacter finds a match at the beginning or end of a word.

If no match is found, it returns null.

The following code shows how to Do a global search for "book2" at the beginning or end of a word in a string:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {//ww w.ja  va 2 s.  c  om
    var str = "Visit java2s.com";
    var patt1 = /\bbook2/g;
    var result = str.match(patt1);
    document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>

Related Tutorials