从字符串中获取整数

时间:2018-02-13 18:28:27

标签: javascript regex

如何使用正则表达式从字符串中获取第二个:后的整数?

请参阅示例:

input: '<a:test:398324781412777419>' ==> output: '398324781412777419'
input: '<a:10test:391278741412111419>' ==> output: '391278741412111419'
input: '<a:test10:123487466611419>' ==> output: '123487466611419'

3 个答案:

答案 0 :(得分:3)

您可以使用正则表达式查找数字和后续>

正则表达式有两部分,

/\d+(?=>$)/
 ^^^         search for digits
    (?=  )   pattern for positive lookahead (the part is not included in the result)
       >$    looks for character > and for end of string

var string = '<a:test:398324781412777419>',
    number = string.match(/\d+(?=>$)/)[0];
    
console.log(number);

答案 1 :(得分:1)

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用.replace(/[^:]?[^:]?(\D)/g, '')

实施例: '<a:test10:398324781412777419>'.replace(/[^:]?[^:]?(\D)/g, '') // will return '398324781412777419'