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

6
  • Please refer this answer here: stackoverflow.com/a/407264/674127 Commented Feb 3, 2014 at 19:22
  • "{132} TestString" should turn into "123 TestString"? Commented Feb 3, 2014 at 19:23
  • 1
    What is the expected output? "{132} TestString".replace(/^{([0-9]+)}.*/i,"$1") would be "132" Commented Feb 3, 2014 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) Commented Feb 3, 2014 at 19:27
  • For some reason escaping the curly bracket did the job,.. Thanks for all your inputs My question is resolved now. Commented Feb 3, 2014 at 19:30

1 Answer 1

1

Escape { and } so use this regex in JS:

/^\{([0-9]+)\}.*/
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.