97-109-107
97-109-107

Reputation: 95

Skipping over tags and spaces in regex html

I'm using this regex to find a String that starts with !?, ends with ?!, and has another variable inbetween (in this example "a891d050"). This is what I use:

var pattern = new RegExp(/!\\?.*\s*(a891d050){1}.*\s*\\?!/);

It matches correctly agains this one:

!?v8qbQ5LZDnFLsny7VmVe09HJFL1/WfGD2A:::a891d050?! 

But fails when the string is broken up with html tags.

<span class="userContent"><span>!?v8qbQ5LZDnFLsny7VmVe09HJFL1/</span><wbr /><span class="word_break"></span>WfGD2A:::a891d050?!</span></div></div></div></div>

I tried adding \s and {space}*, but it still fails. The question is, what (special?)characters do I need to account for if I want to ignore whitespace and html tags in my match.

edit: this is how I use the regex:

var pattern = /!\?[\s\S]*a891d050[\s\S]*\?!/;

document.body.innerHTML = document.body.innerHTML.replace(pattern,"new content");

It appears to me that when it encounters the 'plain' string it replaces is correctly. But when faced with String with classes around it and inside, it makes a mess of the classes or doesn't replace at all depending on the context. So I decided to try jquery-replacetext-plugin(as it promises to leave tags as they were) like this:

$("body *").replaceText( pattern, "new content" );

But with no success, the results are the same as before.

Upvotes: 0

Views: 242

Answers (2)

97-109-107
97-109-107

Reputation: 95

The problem was apparently solved by using this regex:

var pattern = /(!\?)(?:<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])*?>)?(.)*?(a891d050)(?:<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])*?>)?(.)*?(\?!)/;

Upvotes: 0

Christophe
Christophe

Reputation: 28154

Maybe this:

var pattern = /!\?[\s\S]*a891d050[\s\S]*\?!/;

[\s\S] should match any character. I have also removed {1}.

Upvotes: 1

Related Questions