10

I have a string which has a number in it that I would like to replace with another number.

ie

<a href="" id="my_link">blah blah 32 blah blah</a>

I know there is only going to be 1 number in this string.

I can get this far:

var my_string = $('a#my_link').text();

But basically I don't know how to then perform a search on my_string for a numeral and replace that number with something else.

Is that possible with jQuery?

Thanks for any ideas.

7 Answers 7

15

Many jQuery methods like .text() can accept a function that returns the value to insert.

Try it out: http://jsfiddle.net/6mBeQ/

$('#my_link').text( function(i,txt) {return txt.replace(/\d+/,'other value'); });

This removes the need to run the selector twice.

Also, when you are getting an element by its ID, it is actually a little quicker if you do not include the tag name.

So instead of

$('a#my_link')

it is better to do

$('#my_link')

as I did above.

Sign up to request clarification or add additional context in comments.

2 Comments

Now, this is a solution that doesn't require two element grabs. Nice.
I'll need to get into the habit of using functions like that. It's a really neat way when replacing stuff.
7
var new_string = $('a#my_link').text().replace(/[0-9]+/, "somethingelse")

Replace somethingelse with, well, something else. :)

Comments

3
$('a#my_link').text($('a#my_link').text().replace(/\d+/,'something'));

2 Comments

$('a#my_link').text($(this).text().replace(/\d+/,'something')); should be even better
@Mikulas Dite - Try it and you'll discover that this in your example isn't referring to the element you think it is. ;)
2

This will work for simple natural numbers containing 0 - 9.

var my_string = $('a#my_link').text().replace(/[0-9]+/, 'replacement');

If you need to match more complex numbers, such as decimals and negative numbers, then this would work:

var my_string = $('a#my_link').text().replace(/-?[0-9]*\.?[0-9]+/, 'replacement');

If you need to match more complex still, like exponential notation, or numbers with commas, you'd need to modify the regex appropriately -- how you do that will depend on how stringently you want to validate.

2 Comments

That's not what the OP asked. It will return the first character of the number ('3' in this case); he wants to replace the whole number with another number.
Thanks, edited :-) However the regex will match the whole number, not the first digit.
1

If you need extract number and then replace with other value.

let temp ="<polygon points=' 42,4874 936,4874 936,3434 42,3434'></polygon>"
temp.replace(/(\d+)/g,v=>v*10)

multiply by 10 to each number

result will be "<polygon points=' 420,48740 9360,48740 9360,34340 420,34340'></polygon>"

1 Comment

I love this concise and powerful syntax. I'll note that the capture group is not necessary. For my purposes, I needed string.replace(/\d+/g, v=>v.padStart(5,'0'));
0

First you need to loop through each word by splitting off on the space character using the split() function and send the result to a string array.

Once you do that, test each word for the number. If you find the number use the jquery replaceWith() function: http://api.jquery.com/replaceWith/

Hope that helps.

Comments

0
<script>
let text = "1) hit this";
let result = text.match(/[0-9]\)/g);
let result1_ = text.replace(/[0-9]\)/g,'');
document.getElementById("demo").innerHTML = result_;
</script>

1 Comment

Hello, please don't post code only and add an explantation as to why you think that this is the optimal solution. People are supposed to learn from your answer, which might not occur if they just copy paste code without knowing why it should be used.

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.