user1839059
user1839059

Reputation: 3

regex to change text inside a html tag

First of all I'm new to stackoverflow so I'm sorry if I posted this in the wrong section.

I need a regex to search within the html tag and replace the - with a _ e.g:

<TAG-NAME>-100</TAG-NAME>

would become

<TAG_NAME>-100</TAG_NAME>

note that the value inside the tag wasn't affected.

Can anyone help?

Thanks.

Upvotes: 0

Views: 1509

Answers (2)

Martin Ender
Martin Ender

Reputation: 44259

Since JavaScript is the language for DOM manipulation, you should generally consider parsing the XML properly and using JavaScript's DOM traversal functions instead of regular expressions.

Here is some example code on how to parse an XML document so that you can use the DOM traversal functions. Then you can traverse all elements and change their names. This will automatically exclude text nodes, attributes, comments and all other annoying things, you don't want to change.

If it has to be a regex, here is a makeshift solution. Note that it will badly fail you if you have tags (or even only >) inside attribute names or comments (in fact it will also apply the replacement to comments):

str = str.replace(/-(?=[^<>]*>)/g, '_');

This will match a - if it is followed by a > without encountering a < before. The concept is called a negative lookahead. The g modifier makes sure that all occurrences are replaced.

Note that this will apply the replacement to anything in front of a >. Even attribute values. If you don't want that you could also make sure that there is an even number of quotes between the hyphen and the closing >, like this:

str = str.replace(/-(?=[^<>"]*(?:"[^<>"]*"[^<>"]*)*>)/g, '_');

This will still change attribute names though.

Here is a regexpal demo that shows what works and what doesn't work. Especially the comment behavior is quite horrible. Of course this could be taken care of with an even more complex regex, but I guess you see where this is going? You should really, really use an XML parser!

Upvotes: 2

SwiftMango
SwiftMango

Reputation: 15284

s/(\<[^\>]+\>)\-([^\<]+\<\/)/\1_\2/

Although I am not familiar with JS libraries, but I am pretty sure there would be better libraries to parse HTML.

Upvotes: 0

Related Questions