Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove apostrophe

Tags:

regex

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

like image 850
Gerald Ferreira Avatar asked Jul 01 '26 21:07

Gerald Ferreira


2 Answers

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.

like image 187
Peter Boughton Avatar answered Jul 03 '26 14:07

Peter Boughton


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,'')
like image 28
Hammad Khan Avatar answered Jul 03 '26 13:07

Hammad Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!