How to remove special character (:\) from string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 06:47 AM
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
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 09:40 PM
for one back slash it is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 11:18 PM
Hi
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/)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 09:50 PM
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