1

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?

3 Answers 3

3

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 =.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

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

1 Comment

something like this: str.replace(/(=|-)*/, "")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.