>x; cout<< "hey";abc cin>>x...">

Multiple wraps of cout "<<" using regex

I can wrap cout with one occurrence of << , like this:

 cout<< "hey"; abc cin>>x; cout<< "hey";abc cin>>x; 

But I couldn’t wrap it correctly when it is something like this:

 cout<< "hey" << "men";abc cin>>x; cout<< "hey" <<"men"; abc 

Regex

 text = text.replace(/(cout\s*(&lt;&lt;[^;]*)+;)/g, '<span class="group">$1</span>'); 

See : http://jsfiddle.net/3N4AE/23/

+4
source share
1 answer

Your problem was that semicolon in your regular expression also finds a semicolon in &lt; special char.

Fixed using the following regular expression:

 /(cout\s*(&lt;&lt;.*?)[^(&lt;)];)/g 

jsFiddle Demo

+2
source

Source: https://habr.com/ru/post/1499924/


All Articles