Vikas Nimse Vikas Nimse - 21 days ago 11
jQuery Question

Jquery Regex to find Sentence matching in htmlstring

Jquery,Regex to find Sentence matching in htmlstring?


I want to find match for "Original cost basis $0.5 million" and replace it with wrapping span tag around it.
It would be great if i will get Pre match text and post match text also in callback of replace.

var str = '<td><font style="font: 8pt arial, helvetica, sans-serif">Original cost basis $0.5 million</font></td>';

Answer Source

Based on your comment on my previous answer, it seems the problem is not what I read, and the HTML can't really be guaranteed to be in this structure.

Instead, I will assume the string is in the leaf (lowest part) of the HTML tree. Try this:

str.replace(/>([^<]+)</, '<span>$1<\span>');