Javascript 从js中字符串的结尾提取非负数
正如标题所说 示例说明: x) 示例字符串->我需要什么作为输出 示例:Javascript 从js中字符串的结尾提取非负数,javascript,Javascript,正如标题所说 示例说明: x) 示例字符串->我需要什么作为输出 示例: 1) "asdf" -> null or -1 or undefined (because nothing was found) 2) "asdf1" -> 1 3) "asdf0" -> 0 4) "asdf-1" -> 1 5) "asdf001" -> 1 6) "asdf1234" -> 1234 7) "
1) "asdf" -> null or -1 or undefined (because nothing was found)
2) "asdf1" -> 1
3) "asdf0" -> 0
4) "asdf-1" -> 1
5) "asdf001" -> 1
6) "asdf1234" -> 1234
7) "asdf12.34" -> 34 (ending value, so number after .)
8) "123asdf78" -> 78 (integer from ending)
我希望这些例子足够了。我试着用for循环来做这个,但没有成功。有人知道是否有一些函数可以执行类似的操作吗
有关我的方法的更多信息:
在我为每个字符检查的for循环中,如果大于等于“0”&&,则可以使用简单的正则表达式:
函数numericSuffix(字符串){
常量match=string.match(/\d+$/);
返回匹配!==null?编号(匹配[0]):null;
}
numericSuffix('abc-123')//123
numericSuffix('abc-123x')//null
为此,您可以使用简单正则表达式:
函数numericSuffix(字符串){
常量match=string.match(/\d+$/);
返回匹配!==null?编号(匹配[0]):null;
}
numericSuffix('abc-123')//123
numericSuffix('abc-123x')//null
是的,我在文章的结尾写的……只要从反面执行循环,一旦发现一个字符,就将其打断,然后输出提取的数字。好的,请看我的答案。是的,我在文章的结尾写的…只要从反面执行循环,一旦你找到一个字符,将其打断,然后输出提取的数字。好的,那么,请看我的答案。