Rajasekhar
Rajasekhar

Reputation: 69

Regular expression using javascript for removing the special characters

How to write the regular expression for the below string using Javascript.

[Account].&[1]+[Account].&[2]+[Account].&[3]+[Account].&[4]

T need the following output format

1,2,3,4

emoving all the strings and special characters.

Upvotes: 0

Views: 596

Answers (3)

sandamal wijeweera
sandamal wijeweera

Reputation: 51

try this

yourString.replace('[^a-zA-Z0-9 -]', '');

Upvotes: 0

F.P
F.P

Reputation: 17831

/\[(\d+)\]/g

That will return you the array containing all numbers in the string, which you then can join using , as delimiter.

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

Upvotes: 3

sinsedrix
sinsedrix

Reputation: 4775

This will give you an array of the extracted numbers:

var ar1 ='[Account5].&[1]+[Account6].&[2]+[Account7].&[3]'.match(/\[\d+\]/g);

And to assert you extract only numbers between brackets:

var ar2 = ar1.map(function (elt) { return elt.replace(/\[(\d+)\]/, "$1"); });

Upvotes: 0

Related Questions