[英]How to to extract a string between two characters?
These are my strings: 这些是我的字符串:
there_is_a_string_here_1_480@1111111
there_is_a_string_here_1_360@1111111
there_is_a_string_here_1_180@1111111
What I want to do is extracting 180, 360 and 480 from those strings. 我要从这些字符串中提取180、360和480。
I tried this RegEx _(.*)@
but no chance. 我尝试过此RegEx
_(.*)@
但没有机会。
You just want the capture group: 您只需要捕获组:
var str = 'there_is_a_string_here_1_180@1111111'; var substr = str.match(/_(\\d*)@/); if (substr) { substr = substr[1]; console.log(substr); } //outputs 180
you almost got it 你几乎明白了
_(\d{3})@
you need to do a match on digits, or else the string will also get selected because of the other underscore. 您需要对数字进行匹配,否则由于其他下划线,字符串也将被选择。
Ofcourse your match will be in \\1
当然,您的比赛将是
\\1
Try this 尝试这个
var str = "there_is_a_string_here_1_480@1111111"; var matches = str.match(/_\\d+@/).map(function(value){return value.substring(1,value.length-1);}); document.body.innerHTML += JSON.stringify(matches,0,4);
try this: 尝试这个:
(?<= (?!.* ))(.*)(?=@) (?<= (?!。* ))(。*)(?= @)
Use lookbehind (?<=) and look ahead (?=) so that "_" and "@" are not included in the match. 使用lookbehind(?<=)并向前看(?=),以便匹配中不包含“ _”和“ @”。
(?!.* ) gets the last occurence of "_". (?!。* )获得“ _”的最后一次出现。
(.*) matches everything between the last occurence of "_" and "@". (。*)匹配最后一次出现的“ _”和“ @”之间的所有内容。
I hope it helps. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.