简体   繁体   中英

How to remove strings with numbers and special characters using regular expression

Remove strings with numbers and special characters using regular expression.Here is my code

  var string = "[Account0].&[1]+[Account1].&[2]+[Account2].&[3]+[Account3].&[4]";
    var numbers = string.match(/(\d+)/gi);
    alert(numbers.join(','));

here output is : 0,1,1,2,2,3,3,4

But i want the following output 1,2,3,4

Can any one please help me.

Thanks,

Seemd what you want is [\\d+], use exec like this,

var myRe = /\[(\d+)\]/gi;

var myArray, numbers = [];

while ((myArray = myRe.exec(string)) !== null) {
    numbers.push(myArray[1]);
}

http://jsfiddle.net/xE265/

You can do:

string = "[Account0].&[1]+[Account1].&[2]+[Account2].&[3]+[Account3].&[4]";
repl = string.replace(/.*?\[(\d+)\][^\[]*/g, function($0, $1) { return $1 });
//=> "1234"

I guess the simplest solution in this case would be:

\[(\d+)\]

simply saying that you only want the digits enclosed by brackets.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM