0

I have problem in removing ' with (blank and no space). Like Kello's to kellos.

I already tried this-

str = str.replace(/[\']/g, ''); 

But its not working.

Please help.

2
  • its work take a look at JsFiddle. Commented Jul 25, 2015 at 13:31
  • 2
    The regular expression could be simplifed to /'/g but that would not change the already working code. Please be more specific in describing the problem. Input, expected output, actual wrong output. Commented Jul 25, 2015 at 13:31

2 Answers 2

1

It actually does work:

var str = 'aa\'bb\'cc';
alert(str.replace(/'/g,'')); // aabbcc
alert(str.replace(/[\']/g,'')); // aabbcc

You do not need a character class, you just have to mask it if you use single quotes in JavaScript.

Also, keep in mind that ' (U+0027) is different from (U+2019) and must be handled separately.

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

6 Comments

That's the exact same code as shown by @DeveshAgrawal. How is this supposed to be an answer?
@Andreas: It is not, please read again carefully. The first statement emphasizes that it already does work. Also, I’m showing a simpler solution that also works while noting that problems may arise from quotes and/or misinterpreted characters that look like an apostrophe.
The code shown by Devesh already works so this is just a "It works, see...". The "simpler" regular expression doesn't change anything - besides using less characters. And the example in the question has a simple apostroph and not a U+2019. so this part is only a guess which would be a comment.
@Andreas: Objection again: It very well changes something, i.e. leaving out the character class (making a substantial difference for the regex engine) and a masking backslash, being part of my answer. The remark on similiar apostrophes was not only for the sake of completeness, but very much intended, because the OP’s given string (“Kello’s”) is rather the source of error than the obviously working code, especially when somebody is not absolutely sure about notation and is retyping an example. You’re welcome to flag the question as unanswerable or to produce an improved response yourself.
What is the difference between .replace(/'/g, '') and .replace(/[\']/g, '') in respect to the result? Exactly: none. Both replace all ' with a blank.
|
0

A Kello's

str.replace(/[{\ },{\'}]/g,"");

result AKellos

Comments

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.