Rajasekhar
Rajasekhar

Reputation: 2455

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,

Upvotes: 0

Views: 565

Answers (3)

anubhava
anubhava

Reputation: 785196

You can do:

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

Upvotes: 1

SamWhan
SamWhan

Reputation: 8332

I guess the simplest solution in this case would be:

\[(\d+)\]

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

Regards

Upvotes: 0

Andrew
Andrew

Reputation: 5340

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/

Upvotes: 2

Related Questions