0

i'm pretty unconfident with regex and i need to do this simple work: i have an input string like this:

<a id="randomid">some text ( +1 even more text)</a>

now i need to replace that "1", i know it will always be between ( + and a space, how can i do this with regex?

That string is generated by ASP.NET inside a asp:HyperLink component, my first try was to generate that number inside a <span> with know id, but looks like asp.net remove all my html tags inside a ASP component

1
  • Have you tried anything so far? Commented May 15, 2013 at 8:24

2 Answers 2

1

If your assumption is correct, this will also work for you, it will replace the first occurance of "( +number" with "( +replaceNumber".

var regex = /\(\s+\+[0-9]+/
var replaceNumber = 2;
$('a#randomid').text().replace(regex, "( +"+replaceNumber);

TO replace all the occurances change the regex to

var regex = /\(\s+\+[0-9]+/g
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you're using jQuery...

$('a#randomid').text().replace(/(.+ \( \+)([0-9]+)(.+)/, "$12$3")

Will give you...

some text ( +2 even more text)

4 Comments

seems like is not working :( i'm sure the input string is correct, i used a console.log($('a#randomid').text()) and output is right but it doesn't replace the number :(
Using "some text ( +1 even more text)".replace(/(.+ ( \+)([0-9]+)(.+)/, "$12$3") in Chromiums console I get "some text ( +2 even more text)". Works for me.
.. in console works even with me :\ ok i mark this as correct answer, thank you! now i have to figure out why it doesn't work in my code :P
ok i was pretty dumb, i was just omitting the "$('#myid').text() = " eheh, thank you!

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.