Reputation: 1337
I have input text that contains a ' like in this text "Frank's Reel Movie Reviews"
how do I get rid of the '
I have tried
.replace (/\'/ig, '');
.replace ('\'', '');
But it seem like the ' does now want to be deleted...
I am thinking that the ' maybe encoded utf-8 or something
Any ideas
Upvotes: 3
Views: 28207
Reputation: 1143
If you want something very selective to remove (or replace by anything like a space) apostrophes but not the symbol ' for feet imperial unit, use:
val apostropheRegex = """(?<=[a-zA-Z])'(?=[a-zA-Z])"""
"john's carpet is 5' x 8'".replaceAll(apostropheRegex, "XXX") // johnXXXs carpet is 5' x 8'
It means "Replace all symbol ' that are between two letters".
Upvotes: 0
Reputation: 17354
This is late reply but summarizing the answer with quality answer with code addressing different ways of doing it.
You do not need to use escape sequence when detecting apostrophe. The correct regular expression would be
/'+/g
This will remove all apostrophes from the regular expression, if if there are occurrences like ' or '', or ''' and so on.
Here is the code snippet which removes only one instance of apostrophe from a string.
JavasScript
var name = document.getElementById('name').value;
name = name.replace(/'/,'')
alert('The result string ' + name);
PHP
$subject ="Mik's sub";
$resplace = "";
$search ="'";
$new_str = str_replace($search, $replace, $subject);
echo "New Subject : $new_str";
Unicode with JavaScript
var regex = /\u0027/;
name = name.replace(regex,'')
Upvotes: 6
Reputation: 526583
If you just want to have letters and spaces in your result, you could always match any character that isn't one of those, such as...
.replace (/[^a-zA-Z ]+/ig, '');
You could of course also add any other characters you desired to permit to the regex.
Upvotes: 0
Reputation: 98746
The ' should not need to be escaped. Try leaving it naked, without the backslash.
Upvotes: 1
Reputation: 112150
The regex [^\w ]
will match anything that is not alphanumeric or space.
You could use this to ensure all apostrophes/quotes/etc get removed, even if done with Unicode - though there is not enough information in the question to know if this is acceptable.
Upvotes: 6
Reputation: 43560
Assuming you're working with Java, have you tried .replaceAll("'", "")? Works for me.
Upvotes: 2