[英]How to to extract a string between two characters?
這些是我的字符串:
there_is_a_string_here_1_480@1111111
there_is_a_string_here_1_360@1111111
there_is_a_string_here_1_180@1111111
我要從這些字符串中提取180、360和480。
我嘗試過此RegEx _(.*)@
但沒有機會。
您只需要捕獲組:
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
你幾乎明白了
_(\d{3})@
您需要對數字進行匹配,否則由於其他下划線,字符串也將被選擇。
當然,您的比賽將是\\1
嘗試這個
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);
嘗試這個:
(?<= (?!。* ))(。*)(?= @)
使用lookbehind(?<=)並向前看(?=),以便匹配中不包含“ _”和“ @”。
(?!。* )獲得“ _”的最后一次出現。
(。*)匹配最后一次出現的“ _”和“ @”之間的所有內容。
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.