replace character using regex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2016 05:51 AM
I have a string like Building\Floor\Space\
My requirement - I want to replace all '\' with ',' and then split the result like:
Building
Floor
Space
How this can be achieved?? Looks like ServiceNow does not recognize '\' as a regex.
Any lead will be appreciated. Thanks in advance!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2016 05:55 AM
Hello Kumar,
The \ sign is the escape character, and as in ServiceNow it may not produce error, it will someday, somewhere, so use:
var str = 'Building\Floor\Space\'
str.replace(/\\/g, "\n");
Result:
"Building
Floor
Space
"
Notice the double \\, required to escape the code.
It that produces an error, try replacing the / in the original string 'Building\Floor\Space\' with double //, so it will look like 'Building\\Floor\\Space\\', you can do this like str = str.replace(////g, '//');
Regards,
Michal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2016 06:37 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2016 06:56 AM
The problem here is with the back slash characters. You need escape character as well.
Your string should be
var str="Building\\Floor\\Space\\";
str.split('\\');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2020 11:53 AM
How can we replace double quotes ("") with single quotes ('')?