Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex catch string between two strings, multiple lines

I´m working on a *.po file, I´m trying to catch all the text between msgid "" and msgstr "", not really lucky, never more than one line:

msgid ""
"%s asdfgh asdsfgf asdfg %s even if you "
"asdfgdh sentences with no sense. We are not asking  translate "
"Shakespeare's %s Hamlet %s !. %s testing regex %s "
"don't require specific industry knowledge. enjoying "
msgstr ""

What I´ve tried:

var myArray = fileContent.match(/msgid ([""'])(?:(?=(\\?))\2.)*?\1/g);

Thanks for your help, I´m not really good with regex :(

like image 346
lfergon Avatar asked Sep 06 '25 16:09

lfergon


1 Answers

Here is one way to extract all of that text:

var match = text.replace(/msgid ""([\s\S]*?)msgstr ""/, "$1");

Example: http://jsfiddle.net/bqk79/

The [\s\S] is a character class that will match any character including line breaks, so [\s\S]*? will match any number of any character. In other languages you could use the s or DOTALL flag to make . match line breaks, but Javascript does not support this.

Note that you regex doesn't make any mention of single quotes, but if you need to be able to match between msgid '' and msgstr '' as well you can use the following:

var match = text.replace(/msgid (['"]{2})([\s\S]*?)msgstr \1/, "$2");
like image 68
Andrew Clark Avatar answered Sep 09 '25 06:09

Andrew Clark