2

I have the text:

s.events="event3"
s.pageName="Forum: Index"
s.channel="forum"
s.prop1="Forum: Index"
s.prop2="Index Page"
s.prop36=""
s.prop37=""
s.prop38=""
s.prop39=""
s.prop40="53"
s.prop41="Anonymous"
s.prop42="username"
s.prop43=""
s.prop47=""
s.eVar1="Forum: Index"
s.eVar2="Index Page"
s.eVar36=""
s.eVar37=""

saved in a var in javascript and I want to extract the text between the quotes of s.prop42 giving me the result:

"username"

what I have right now is

    var regex = /\?prop.42="([^']+)"/;
    var test = data.match(regex);

but it doesnt seem to work, can someone help me out?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Kyle Asaff
  • 274
  • 3
  • 13
  • Why are you trying to match a literal question mark? Why are you not allowing *single* quotes in a *double*-quote terminated string? – Bergi Jul 23 '14 at 02:59

2 Answers2

2

Use this:

var myregex = /s\.prop42="([^"]*)"/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
    thematch = matchArray[1];
} 

In the regex demo, look at the capture group in the right pane.

Explanation

  • s\.prop42=" matches s.prop42=" (but we won't retrieve it)
  • The parentheses in ([^"]*) capture any chars that are not a " to Group 1: this is what we want
  • The code gets the Group 1 capture
zx81
  • 41,100
  • 9
  • 89
  • 105
0

Can't comment on above answer, but I think the regex is better with .* like so:

var myregex = /s\.prop42="(.*)"/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
    thematch = matchArray[1];
}
Michael Y.
  • 661
  • 7
  • 12