views:

43

answers:

4

I've a document from which I need to extract some data. Document contain strings like these

Text:"How secure is my information?"

I need to extract text which is in double quotes after the literal Text:

How secure is my information?

How do I do this with regex in Javascript

A: 

JavaScript regular expressions do not support lookbehinds. You have to use capturing parenthesis instead:

var str = 'Text:"How secure is my information?"',
    reg = /Text:"([^"]+)"/g;
alert(str.match(reg)[1]);
// -> How secure is my information?
Andy E
But how do I extract all such data from a large document? Say into an array or something?
Raj
You use the /g modifier like what I had in my answer.
Crayon Violent
@Raj: you can use the `/g` modifier, as Crayon Violent said. This is the global modifier, without it the regular expression will stop execution when it finds the first match, with it the regular expression continues until it finds all matches, returning an array with all the matches in it.
Andy E
+1  A: 
string.match(/Text:"([^"]*)"/g)
Crayon Violent
How to avoid matching Text: in the result?
Raj
string[0] will always have the full regex match. string[1] will have the captured text. If there was a 2nd capture (parenthesis) in the regex, it would be put in string[2], etc...
Crayon Violent
I think with /g flag you will only get full regex match for all matches.
Raj
The g modifier will look for everything on the page that matches, not just stop at the first match. You said you have strings (plural), so that's why I put the /g modifier there.
Crayon Violent
+2  A: 

You can just do:

/Text:"(.*?)"/

Explanation:

  • Text:" : To be matched literally
  • .*? : To match anything in non-greedy way
  • () : To capture the match
  • " : To match a literal "
  • / / : delimiters
codaddict
How do you use this? Do you have some context?
Sjoerd
that is the regex, you are supposed to use it with for instance .match()
Crayon Violent
A: 
<script type="text/javascript">
var str = 'Text:"How secure is my information?"';
var obj = eval('({'+str+'})')
console.log(obj.Text);
</script>
Sjoerd