0

I have this string

@test <span class="mention">@test</span> @test2 <span class="mention">@test</span>`

And I would like to match all words starting with an @ symbol outside the span in Javascript

I have created the RegExp for matching the span objects

/<span class="mention"((.*)data-mention="\d+")?>@\w+<\/span>/g

But I just can't work out how to get elements not inside the span element!

4
  • 2
    Why only a regular expression? HTML is not a regular language, it can't be reliably parsed by individual regular expressions. Commented Aug 28, 2014 at 3:17
  • 1
    you may perhaps shorten your regex to /<span[^>].*?<\/span>/g Commented Aug 28, 2014 at 3:25
  • @RobG the library im using only supports regex for these things.. Commented Aug 28, 2014 at 3:27
  • @MichaelH how about this regex101.com/r/dR4bN1/3 ? Commented Aug 28, 2014 at 3:28

2 Answers 2

2

here is a try to extract the desired

Regex

/(@\w+\b)(?![^>]*?<\/span>)/g

Test String

@test <span class="mention">@test</span> @test2 <span class="mention">@test</span>`

Result

  • MATCH 1
      1. [0-5] @test
  • MATCH 2
      1. [41-47] @test2

Demo

try demo here

note that it is not a foolproof solution, it is simply based on your test string.

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

3 Comments

Can you help me with one more? Im trying to strip all html tags except those that are <span class="mention">any word</span>, any suggestions?
you can try /(<span[^>].*?<\/span>)/g (demo) to replace every span with text, or /(<span class="mention".*?\/span>)/g (demo) if you want to replace only span with the specified class attribute.
I actually dont want to replace the span if it has a class.. but want to remove all html tags besides that one stackoverflow.com/questions/25541441/…
0

Don't use a regex. It's HTML, so use the DOM manipulation methods:

var s = '@test <span class="mention">@test</span> @test2 <span class="mention">@test</span>';

var e = document.createElement("div");
e.innerHTML = s;
var spans = e.getElementsByTagName("span");
while (spans.length > 0) {
    e.removeChild(spans[0]);
}
console.log(e.innerHTML);

http://jsfiddle.net/mzny7857/

This will leave you with @test @test2 which you can then split or match with a regex.

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.