Reputation: 4507
regOpenTags = new RegExp("<+[a-zA-Z]", "g");
matches html opening tags (e.g. <body>
) without matching closing tags (e.g. </body>
).
I'd like to replace the first character of matched tags which is <
, but doing
parsedString = string.replace(regOpenTags, '');
would replace also the first letter. In other words, when using replace()
on a string containing <body>
, I'd prefer to get body>
instead of ody>
.
How can I use the replace()
method or regex to achieve this?
Upvotes: 0
Views: 5049
Reputation: 70722
You can use a Positive Lookahead to achieve this.
regOpenTags = new RegExp("<(?=[a-z])", "gi");
Regular expression
< '<'
(?= look ahead to see if there is:
[a-z] any character of: 'a' to 'z'
) end of look-ahead
See working demo
Another way of using a Positive Lookahead with negation.
regOpenTags = new RegExp("<(?=[^/])", "g");
Upvotes: 3