RegularExpression String replace

I have html code as a string variable

like a = "<span> Spain will win ....... </span>"

and I want to heightlight "spa" in the String.

I have to use RegExp, How should I write the pattern that it ignores the "spa" on span tag, but highlight in Spain.

Thanks

Answers


JavaScript doesn't support lookbehind, so you're a bit limited, but this should work:

\bSpa(?!n[^>]*>)

Explanation:

\b: Assert that the match starts at a word boundary.

Spa: Match Spa.

(?!...): Assert that it is impossible to match the following:

n: An n, followed by...

[^>]*: zero or more non-> characters, followed by...

>: a >.


You can try this:

var s = "<span> Spain will win ....... </span>Spaniel";
s.replace(/(spa(?!n[^>]*>))/ig,'<span class="highlight">\$1</span>')

Need Your Help

local storage data to server

javascript ajax json local-storage

I happen to have a feature on my web app that saves data into local storage, which I then convert into a json object. Is there a way for me to pass the local storage data to my server using ajax. I...

Passing user input data from view to controller via AJAX in Laravel 5.1

php jquery ajax laravel

I want to send drop-down list data from view to controller via AJAX as a form variable using post method.