Satya
Satya

Reputation: 525

need help in finding javascript code

I am facing this problem. i am getting strings like this.

'=--satya','=---satya1','=-----satya2'.

now my problem is i have to remove these special characters and print the strings like this

'satya'
'satya1'
'satya2'

please help to solve this problem?

Upvotes: 1

Views: 60

Answers (3)

Jamiec
Jamiec

Reputation: 136124

You could extract that information with a regular expression such as

/\'\=-{0,}(satya[0-9]{0,})\'/

Live example: http://jsfiddle.net/LFZje/

The regex matches

Literal '
Literal =
Zero or more -
Starts a capture group and captures
- Literal satya
- Zero or more numbers
Ends the capture group
Literal '

Then using code such as

var regex = /\'\=-{0,}(satya[0-9]{0,})\'/g;
while( (match = regex.exec("'=--satya','=---satya1','=-----satya2'")) !== null)
{
    // here match[0] is the entire capture
    // and match[1] is tthe content of the capture group, ie "satya1" or "satya2"
}

See the live example more detail.

Upvotes: 1

genesis
genesis

Reputation: 50976

Use javascript function replace which helps you to use regex for this case

var string = '=---satya1';
string = string.replace(/[^a-zA-Z0-9]/g, '');

Upvotes: 0

Grzegorz Rożniecki
Grzegorz Rożniecki

Reputation: 28005

Use String.replace:

var s = '=---satya1';
s.replace(/[^a-zA-Z0-9]/g, '');

to replace all non-letter and non-number characters or

s.replace(/[-=]/g, '');

to remove all - and = characters or even

'=---satya-1=test'.replace(/(=\-+)/g, ''); // out: "satya-1=test"

to prevent removing further - or =.

Upvotes: 3

Related Questions