1

i confused with this code

<!DOCTYPE html>
<html>

    <body>
        <p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph
            below:</p>
        <p id="demo">Microsoft Visit Microsoft! Microsoft visit visit microsoft Visit Visit
            Visit Visit</p>
        <button onclick="myFunction()">Try it</button>
        <script>
            function myFunction() {
                document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML.replace(/ Visit |Microsoft/gi, ' test ');
            }
        </script>
    </body>

</html>

when click on button some words didn't change,if click again will change

after first click

test test test ! test test visit test test Visit test Visit 

after second click

test test test ! test test test test test test test test 

why some words didn't change in first time? sincerely

2
  • What exactly are you trying to make? RegEx? Commented Jan 27, 2013 at 16:36
  • @Салман yes i developing a toolbar and inject some thing to pages Commented Jan 27, 2013 at 17:34

3 Answers 3

2

This happens because in your #demo contents two not replaced Visit words do not have space after it (the first has new line, the second has line end).

In order to fix it, instead you may use \b as word boundary: /\bVisit\b|Microsoft/gi.

DEMO: http://jsfiddle.net/dyz5w/

Sign up to request clarification or add additional context in comments.

1 Comment

Even though your solution works, the problem is not caused by the new line and the line end, but rather by what Justin Morgan mentioned in his answer. If the line end would be a problem, then how does the second click replace that Visit? If I use the OP's original regex in your jsfiddle, the second click doesn't replace the last two Visits. So I think the OP's html probably has some spaces around the visits before the new and end line.
1

It's happening because of the spaces around Visit in your regex:

 / Visit /

That requires a space on either side of the word "visit". Regex matches don't overlap, so if you have a string like

  Visit Visit Visit Visit

....then both of the spaces between each instance of "visit" can only belong to a single match. So your regex matches will look like this (using { to show where a match begins and } to show where it ends):

 { Visit }Visit{ Visit }Visit

Try it with two spaces between the "Visit"s. You'll see that it gets them all.

Edit: VisioN beat me to it; his suggestion of using \b is perfect if you want to match only the words.

1 Comment

i want to search for words and \b is good choice for me thanks
0

/ Visit |Microsoft/gi simply means

  • find " Visit " (with spaces around)
  • | (or)
  • find "Microsoft"
  • the g flag - find all the matches
  • the i flag - make a case insensitive search

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.