Regex to remove apostrophe

前端 未结 7 624
野性不改
野性不改 2021-01-22 13:10

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         


        
7条回答
  •  逝去的感伤
    2021-01-22 14:01

    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,'')
    

提交回复
热议问题