Jump to content

problem with metacharacter \b


Nati323

Recommended Posts

in the reference http://www.w3schools.com/jsref/jsref_regexp_begin.asp, you say that b search for a word at the beginning and the end of a word, i try it but its not work on the end of a word, why is that?

the code:

    var str = "Visit W3SchoolsW3";     var patt1 = /(bW3)/g;    var result = str.match(patt1);    document.getElementById("demo").innerHTML = result;

And with a big B, its works but you say there that a big B search a match that is not at the start or end of a word:

var str = "Visit W3SchoolsW3";var patt1 = /BW3/g;var result = str.match(patt1);document.getElementById("demo").innerHTML=result;
Edited by Nati323
Link to comment
Share on other sites

var patt1 = /(bW3)/g;

You're telling it to match a character that is at the end of the word and followed by "W3". If it is followed by any letter then it is not at the end of the word, so that will never match.
Link to comment
Share on other sites

My mistake on that last explanation, that wasn't correct.

i telling to regexp to search W3 at the end of the word

No, you're telling it to find a word boundary followed by W3. That will match at the beginning of a word, not the end. If you want to match the end then you need to look for W3 followed by a word boundary.
Link to comment
Share on other sites

A boundary is a border. The meaning of b isn't an actual character, it will actually match the position between the start or end of a word and a space character. Think of it like the cursor blinking when you type. When you put the cursor at the beginning or end of a word, that is the position that b will match. So bW will match a word that begins with W (the match will be just before the W), and Wb will match a word that ends with W (the match will be just after the W).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...