jQuery 用jquery替换字符串中的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3378300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacing a number in a string with jquery
提问by willdanceforfun
I have a string which has a number in it that I would like to replace with another number.
我有一个字符串,里面有一个数字,我想用另一个数字替换它。
ie
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.
我知道这个字符串中只会有 1 个数字。
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.
但基本上我不知道如何在 my_string 上搜索数字并将该数字替换为其他数字。
Is that possible with jQuery?
用jQuery可以吗?
Thanks for any ideas.
感谢您的任何想法。
回答by user113716
Many jQuery methods like .text()
can accept a function that returns the value to insert.
许多 jQuery 方法.text()
都可以接受一个函数,该函数返回要插入的值。
Try it out:http://jsfiddle.net/6mBeQ/
试试看: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.
此外,当您通过 ID 获取元素时,如果不包含标签名称,实际上会更快一些。
So instead of
所以代替
$('a#my_link')
it is better to do
最好这样做
$('#my_link')
as I did above.
就像我上面所做的那样。
回答by Krevan
var new_string = $('a#my_link').text().replace(/[0-9]+/, "somethingelse")
Replace somethingelse
with, well, something else. :)
用somethingelse
别的东西代替。:)
回答by David Hellsing
$('a#my_link').text($('a#my_link').text().replace(/\d+/,'something'));
回答by Jhong
This will work for simple natural numbers containing 0 - 9.
这适用于包含 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.
如果您仍然需要匹配更复杂的,例如指数符号或带逗号的数字,您需要适当地修改正则表达式——您如何做将取决于您想要验证的严格程度。
回答by Aedaeum
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.
首先,您需要通过使用 split() 函数拆分空格字符来遍历每个单词,并将结果发送到字符串数组。
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/
完成后,测试每个单词的数字。如果您找到该数字,请使用 jquery replaceWith() 函数:http://api.jquery.com/replaceWith/
Hope that helps.
希望有帮助。
回答by SUDHIR KUMAR
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
每个数字乘以 10
result will be "<polygon points=' 420,48740 9360,48740 9360,34340 420,34340'></polygon>"
结果将是 "<polygon points=' 420,48740 9360,48740 9360,34340 420,34340'></polygon>"