shivam dixit
shivam dixit

Reputation: 41

Regex to detect semi-colon in strings

I have a string to detect semicolons from. Only those semicolons which are not inside HTML tags should be matched.

The best regex I have come up with is:

/(^|>)[^<>]*?;[^<>]*?(<|$)/

The above regex I provided works well if I want to detect ; between HTML tags <> and the values are on separate lines, like this:

Value1;
<p style="color:red; font-weight:400;">Value2</p>;
<p style="color:red; font-weight:400;">Value3;</p>;
Value4

However, it fails if everything is on a single line, like:

Value1;<p style="color:red; font-weight:400;">Value2</p>;<p style="color:red; font-weight:400;">Value3</p>;Value4

What am I missing?

Upvotes: 1

Views: 703

Answers (1)

David784
David784

Reputation: 7464

Your problem is more with the code implementation than with the RegExp. A few minor modifications to the capturing groups, but that's it.

The problem is an if loop like that won't really work. You just need to use a single .replace. Try this:

const value = 'Value1;<p style="color:red; font-weight:400;">Value2</p>;<p style="color:red; font-weight:400;">Value3</p>;Value4';

const newVal = value.replace(/((?:^|>)[^<>]*);([^<>]*(?:<|$))/g, '$1\n$2');
console.log(newVal);

Here are the mods I made to the RegExp:

  • make the "or" conditions non-capturing groups (?:)
  • make capturing groups for the parts before/after the semicolon
  • remove unneeded ?: * means zero or more...so you don't need it. (You could have it, but it's not needed).

Upvotes: 1

Related Questions