SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dhtml Utopia Reg Exp

    Hi,

    The regular expression below found in DHTML Utopia book replaces the word "hi" with a blank string.

    parent_row.className = parent_row.className.replace(/\b ?hi\b/, '');

    Could someone please explaning to me why we need to use a word boundary here?

    1 - why this will not suffice?

    parent_row.className = parent_row.className.replace(/hi/, '');

    2 - When should I use a word boundary? I have read some articles online but they were not clear to me.

    3 - What does the className string should look like in this case?

    Cheers

    C

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    without the word boundary you will replace hit with t and this with ts.
    The ' ?' seems superfluous, the browser will tighten up any multiple spaces to one.

    And your regexp will only replace the first instance of hi, unless you flag it with /g.
    If you always use only one classname it may not matter.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi mrhoo,

    Thanks for your reply.

    I did not quite understand the bit you explained the word boundary:

    "without the word boundary you will replace hit with t and this with ts."

    The word is "hi" not "Hit".

    Are you saying that without the word boundary it would only match the end of the string. So in this case it would be the "i"?

    I would appreciate if you could elaborate a bit more on that.

    How should the className string look like for the word boundary to kick off?

    Cheers

    C

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by cleytonjordan View Post
    I did not quite understand the bit you explained the word boundary:

    "without the word boundary you will replace hit with t and this with ts."

    The word is "hi" not "Hit".

    Are you saying that without the word boundary it would only match the end of the string. So in this case it would be the "i"?

    I would appreciate if you could elaborate a bit more on that.

    How should the className string look like for the word boundary to kick off?
    If the class was "white hi" the first occurrence only would change, resulting in "wte hi"
    That is why the word boundary is needed. With the word boundary, only complete words will be matched, not partials.

    A word boundary is exactly what it says. It's the places that separate the word characters from other characters, as well as the start and end of the string.

    With the phrase "This is a test, of the 'word boundary' system" I'll use a * to indicate all of the word boundary locations.
    "*This* *is* *a* *test*, *of* *the* '*word* *boundary*' *system*"

    With the regex /\b ?hi\b/ it will change "white hi" to "white"
    And with "hi white" it will take out "hi" (the " ?" in the regex is for an optional space) leaving " white"

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi pmw57,

    Many thanks for your help and reply.

    I think I now understand that regular expression.

    Cheers

    C

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi pmw57,

    Just one last point:

    You said that the word boundary also match the start and end of the string.

    "A word boundary is exactly what it says. It's the places that separate the word characters from other characters, as well as the start and end of the string."

    So, am I right to assume that we can use word boundary instead of the "^" and $ characters to match the begining and end of a string?

    The example below would be wrong because \b would match the begining of the string (so we do not need the ^) and the second \b would match the end of the string (so we do not need the $). Is that right?

    className.replace(/^\b ?hi\b$/, '');

    Cheers

    C

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by cleytonjordan View Post
    So, am I right to assume that we can use word boundary instead of the "^" and $ characters to match the begining and end of a string?

    The example below would be wrong because \b would match the begining of the string (so we do not need the ^) and the second \b would match the end of the string (so we do not need the $). Is that right?

    className.replace(/^\b ?hi\b$/, '');
    With the ^ in there, the match will have to occur at the very start of the string. "hidden hi" won't have a valid match.

    If you leave out the ^ then the match will occur at the first occurrence in the string. "hidden hi" will have a valid match.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi pmw57,

    Thanks.

    Your explanation was very helpfull and clear

    Cheers

    C


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •