views:

167

answers:

2

Hi all i need your help ! i wanna write javascript function to change the text Lasvegas in string: ex:

"Hello every one in Lasvegas, come <a href='xxx'>Lasvegas</a> with me"

How can i change the text but not change content Lasvegas in start tag and end tag

+1  A: 

May be something like that

str.replace(/Lasvegas[^<]/,'123')
Alexey Ogarkov
This would only work if `Lasvegas` didn't come at the end of the string and your regex may need to be global (`g` modifier) if more than one occurrence needs replacing.
Andy E
it check ok! but when it contant "Lastvegas" with some character other in start and end tag it error !ex: <a>Lastvegas abc ef</a> it wrong
TuanTDA
A: 

EDIT: Forgot JavaScript supports negative lookaheads:

"Hello every one in Lasvegas, come <a href='xxx'>Lasvegas</a> with me"
    .replace(/(Lasvegas(?!<))/g, "World");

Returns:

Hello every one in World, come <a href='xxx'>Lasvegas</a> with me


Left in so everyone can see how pointless my first answer was ;-)

Alexey's answer would work if "Lasvegas" wasn't at the end of the string. I'm not sure if there's a better way, but you could use a replacer function to assert whether the text is inside a tag or not:

var str = "Hello every one in Lasvegas, come <a href='xxx'>Lasvegas</a> with me";
var out = str.replace(
            /(>?Lasvegas<?)/g,
            function ($0) 
            { 
                if ($0.slice(0,1) == ">") 
                    return $0; 
                else
                    return "World";
            }
);

contents of out:

Hello every one in World, come <a href='xxx'>Lasvegas</a> with me

Andy E
i still have some problems with it, when i put the same character in attribute of tag. Ex: <a href='Lasvegas abc'>Lasvegas Text</a>. it false. Please help !Thanks
TuanTDA
@TuanTDA: The `(?!<)` part of my regular expression is a negative lookahead. It will match the content that precedes that lookahead only if the content in the lookahead does not follow the matched text. If you change the lookahead to `(?!(?:[^<]*<)|(?:[^'>]*'>))`, it should do a better job.
Andy E
Sorry ! for spent your time ! if you don't mind please help last question. when i apply (?!(?:[^<]*<)|(?:[^'>]*'>) i still meet error ! all text in attribute not replace but it only replace the last text of string. you can check below for detail.I do as below:var str = "<br/>Visit <a href='Microsoft' class='Microsoft abc'>Microsoft abc</a> check Microsoft and replace with Tuan string <a href='[email protected]'> Microsoft abc</a> welcome to Microsoft";var out = str.replace(/(Microsoft(?!(?:[^<]*<)|(?:[^'>]*'>)))/g, " Tuan ") document.write(out); thanks
TuanTDA
Although JavaScript has lookahead, there are bugs in IE that make it go wrong in weird ways. Hence it is best avoided.
bobince
@bobince: I'll try and remember that. I did test the regex I added to the answer and it worked just fine with the input in the question, although it's hardly a complicated regex by any standard.
Andy E
Yeah, I haven't worked out what the exact circumstances are that cause JScript's regexp lookahead to freak out, so I tend to avoid it altogether. Some background: http://regexadvice.com/blogs/mash/archive/2009/02/21/Looking-again-at-the-Lookahead-bug.aspx
bobince