ClaraU
ClaraU

Reputation: 957

match text between two html custom tags but not other custom tags

I have something like the following;-

<--customMarker>Test1<--/customMarker>
<--customMarker key='myKEY'>Test2<--/customMarker>
<--customMarker>Test3 <--customInnerMarker>Test4<--/customInnerMarker> <--/customMarker>

I need to be able to replace text between the customMarker tags, I tried the following;-

str.replace(/<--customMarker>(.*?)<--\/customMarker>/g, 'item Replaced')

which works ok. I would like to also ignore custom inner tags and not match or replace them with text.

Also I need a separate expression to extract the value of the attribute key='myKEY' from the tag with Text2.

Many thanks

EDIT actually I am trying to find things between comment tags but the comment tags were not displaying correctly so I had to remove the '!'. There's a unique situation that required comment tags... in anycase if anyone knows enough regex to help, it would be great. thank u.

Upvotes: 2

Views: 1157

Answers (3)

Spencer Rathbun
Spencer Rathbun

Reputation: 14910

A regex merely matches an item. Once you have said match, it is up to you what you do with it. This is part of the problem most people have with using regular expressions, they try and combine the three different steps. The regex match is just the first step.

What you are asking for will not be possible with a single regex. You're going to need a mini state machine if you want to use regular expressions. That is, a logic wrapper around the matches such that it moves through each logical portion.

I would advise you look in the standard api for a prebuilt engine to parse html, rather than rolling your own. If you do need to do so, read the flex manual to get a basic understanding of how regular expressions work, and the state machines you build with them. The best example would be the section on matching multiline c comments.

Upvotes: 0

ClaraU
ClaraU

Reputation: 957

In the end, I did something like the following (incase anyone else needs this. enjoy!!! But note: Word about town is that using regex with html tags is not ideal, so do your own research and make up your mind. For me, it had to be done this way, mostly bcos i wanted to, but also bcos it simplified the job in this instance);-

var retVal = str.replace(/<--customMarker>(.*?)<--\/customMarker>/g, function(token, match){
   //question 1: I would like to also ignore custom inner tags and not match or replace them with text.
   //answer:
   var replacePattern = /<--customInnerMarker*?(.*?)<--\/customInnerMarker-->/g;
   //remove inner tags from match
   match = $.trim(match.replace(replacePattern, ''));
   //replace and return what is left with a required value
   return token.replace(match, objParams[match]);

   //question 2: Also I need a separate expression to extract the value of the attribute key='myKEY' from the tag with Text2.
   //answer
   var attrPattern = /\w+\s*=\s*".*?"/g;
   attrMatches = token.match(attrPattern);//returns a list of attributes as name/value pairs in an array    

})

Upvotes: 2

Midas
Midas

Reputation: 7131

Can't you use <customMarker> instead? Then you can just use getElementsByTagName('customMarker') and get the inner text and child elements from it.

Upvotes: 0

Related Questions