Regular expression, selects a piece of text inside another

I am a little unfamiliar about the next task. I want to select text between "it is inside the tag, but not outside the tag, ie The choice is inside another choice.

I have the following tag: <| and | >, and I want to select the text only if it is between "and between tags."

<| blah blah blah " must be selected " not selected " select it too " | > "also not selected"

I think something about

(\<\|)(\").*?(\")(\|\>) 

But that will not work.

+7
javascript c # regex
source share
4 answers

This will do the job in one regex:

(?<=<\|[^>]*)"[^"]*"

In addition to the nicael comment: It is possible that the input line is not marked correctly. This will help:

(?<=<\|((?!\|>).)*)"[^"]*"

If you need to use it with JavaScript:

(?=("[^"]*"[^"]*)*$)"[^"]*"(?=((?!<\|).)*\|>)

+4
source share

I need it to match correctly using two regular expressions.

 var input = '<|a "b"|>c "d"ef<|"g"h "i"|>"j"k l'; var output=input.match(/<\|(.*?)\|>/g) .map(function(x){return x.match(/"(.*?)"/g)}) alert(output) 

As you can see, matches "b", "g", "i" correctly.

Principle:

  • find all text matches between <| and |>
  • for each match from step one, find the text matches between the two quotation marks.

(regex from second answer from related question is used)

+6
source share

I can't think of a regex to match what you want with one shot , but I see no reason not to do this with two regexes:

 var SAMPLE_STRING = '<| blah blah blah "should be selected" not selected "select it too" |> "not selected too" <| "select it" do not select this |> "don\'t select this one too"'; var matchAll = function matchAll(regexp, str) { var lastIndex = regexp.lastIndex; regexp.lastIndex = 0; var result = []; var match; while ((match = regexp.exec(str)) !== null) { result.push(match[0]); } regexp.lastIndex = lastIndex; // so this method won't have any side effects on the passed regexp object return result; }; var withinTagsRegexp = /<\|([^|]|\|[^>])+\|>/g; var withinQuotesRegexp = /"[^"]+"/g; var withinTagsAndQuotes = [].concat.apply([], // flattens the following matchAll(withinTagsRegexp, SAMPLE_STRING).map( matchAll.bind(undefined, withinQuotesRegexp))); // show the result var resultTag = document.getElementById('result'); withinTagsAndQuotes.forEach(function(entry) { var p = document.createElement('p'); p.innerHTML = entry; resultTag.appendChild(p); }); 
 <div id="result"></div> 
+3
source share

Try with peeps and glances:

 (?<=\<\|.)(\"[^"]*\")(?=.\|\>) 

Regular expression visualization

Here is a live demonstration .

+2
source share

All Articles