0

While searching for my answer I found this Is there an equivalent in jQuery to PHP's `preg_replace()`?

But the thing is I want to actually match an pattern and then take the content in it and put it in a other var.

Ok.. i think I'm confusing people more than anything..

this is the code that would work in php.

$intBetweenTheTwoCurlyBraket = preg_replace("/^{([0-9]+)}.*/",'$1',"{132} TestString");

Right now what i have is.

 intBetweenTheTwoCurlyBraket = item.value.replace(/^{([0-9]+)}.*/i,"$1");

But it doesnt work. What im I doing Wrong.

Thanks for any help!


For some reason escaping the curly bracket did the job,..

Thanks for all your inputs My question is now resolved

Community
  • 1
  • 1
Nicolas Racine
  • 1,031
  • 3
  • 13
  • 35
  • Please refer this answer here: http://stackoverflow.com/a/407264/674127 – Anunay Feb 03 '14 at 19:22
  • "{132} TestString" should turn into "123 TestString"? – dfsq Feb 03 '14 at 19:23
  • 1
    What is the expected output? `"{132} TestString".replace(/^{([0-9]+)}.*/i,"$1")` would be `"132"` – epascarello Feb 03 '14 at 19:24
  • your js regex is already identical to the php version (except the `i` modifier, which isn't necessary). If it's not working, your issue is somewhere else, like not pulling the right value to replace (`item.value`) – CrayonViolent Feb 03 '14 at 19:27
  • For some reason escaping the curly bracket did the job,.. Thanks for all your inputs My question is resolved now. – Nicolas Racine Feb 03 '14 at 19:30
  • @NicolasRacine: bracket have special meaning in regexes, You must escape them. – Toto Feb 04 '14 at 08:24

1 Answers1

1

Escape { and } so use this regex in JS:

/^\{([0-9]+)\}.*/
anubhava
  • 761,203
  • 64
  • 569
  • 643