[英]How to remove strings with numbers and special characters using javascript regular expressions?
[英]How to remove strings with numbers and special characters using regular expression
使用正則表達式刪除帶有數字和特殊字符的字符串。這是我的代碼
var string = "[Account0].&[1]+[Account1].&[2]+[Account2].&[3]+[Account3].&[4]";
var numbers = string.match(/(\d+)/gi);
alert(numbers.join(','));
這里的輸出是:0,1,1,2,2,3,3,4
但是我想要以下輸出1,2,3,4
誰能幫幫我嗎。
謝謝,
Seemd您想要的是[\\ d +],像這樣使用exec,
var myRe = /\[(\d+)\]/gi;
var myArray, numbers = [];
while ((myArray = myRe.exec(string)) !== null) {
numbers.push(myArray[1]);
}
你可以做:
string = "[Account0].&[1]+[Account1].&[2]+[Account2].&[3]+[Account3].&[4]";
repl = string.replace(/.*?\[(\d+)\][^\[]*/g, function($0, $1) { return $1 });
//=> "1234"
我想在這種情況下最簡單的解決方案是:
\[(\d+)\]
簡單地說,您只想要用方括號括起來的數字。
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.