How to remove special character (:\) from string

Vinayak Nikam
Kilo Contributor

Hi All,

Need a script to remove special character specially ":\" from string. Due to this special characters my API is not working.

I have tried below scripts but not working please suggest.

 

var str = "F:\";
var test = str.replace(/[^\d\w]/gi, '');
gs.print("Str: " + str + "\ntest: " + test);

---------------------------------------------------------------------

var str = "F:\";
var test = str.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
gs.print("Str: " + str + "\ntest: " + test);

 

Thanks in Advance..

Vinayak

12 REPLIES 12

for one back slash it is not working

 

Hi @Vinayak Nikam,

How are you getting that string with one back-slash? A string with one back-slash can't be a valid string literal, so all functions that you'd try to apply would fail on it. You'd need to first replace/escape it properly at the source, where you're assigning the value to the string. And after that, you can use the code that I've mentioned above to replace ":\\" with "".

 

Thanks & Regards,

Rishabh Jha

Aavenir (https://www.aavenir.com/)

Gaurav Shirsat
Mega Sage

Hi Vinay

below is the Raw Java Script code to remove the Character.This might help you.

<!DOCTYPE html>

<html>

 

<head>

    <title>

        How to remove a character from 

        string using Javascript?

    </title>

</head>

 

<body>

    <h1 style="color: green">

        GeeksforGeeks

    </h1>

     

    <b>

        How to remove a character from

        a string using Javascript?

    </b>

     

    <p>Original string is GeeksforGeeks</p>

     

    <p>

        New String is: 

        <span class="output"></span>

    </p>

 

    <button onclick="removeCharacter()">

        Remove Character

    </button>

     

    <script type="text/javascript">

        function removeCharacter() {

            originalString = 'GeeksForGeeks';

            newString = originalString.replace('G', '');//add your data here

 

            document.querySelector('.output').textContent 

                    = newString;

        }

    </script>

</body>

 

</html>

 

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat