I'm trying to figure out some JavaScript regex that will match the last space that is not inside an HTML tag. For example, in the following example:
// Should match the space between `custom` and `text`
My custom text;
// Should match the space between `a` and `link`
My custom text with <a href="#">a link<a/>.
// Should still match the space between `a` and `link`
My custom text with <a href="#">a link<a/><span style="color: red;">.</span>
I have the following regular expression (source, modified) that selects all spaces not in HTML tags: (?<!<[^>]*)\s(?<![^>]*<)
, but I'm not sure how to take it the last little bit further and select only the last of those spaces.
At first I thought I could do this: (?<!<[^>]*)\s(?<![^>]*<)(?=[^\s]*$)
, but that doesn't work with my last example.
Here's a fiddle.
Any ideas?