0

I have the following strings:

school\boy
school\class
school\playground
school\teacher
school\subject

I want to extract the substring after the \ i.e., boy, class, playground, teacher, subject. Also, school remains a constant substring for all strings.

I know the solution is to add in an extra backslash like school\\boy, school\\teacher etc. since it's treating \b,\t as one special character, but the above data is coming from the back-end service so the solution to add the extra backslash manually for every string is not feasible.

My question is how can I insert this extra \ backslash through string functions or regex and extract the substrings after the \?

I tried doing this:

var str = "school\boy";
var newstr = str.slice(0,6) + '\\' + str.slice(6); \\ newstr = school\\boy
console.log(newstr.split('\\\\')[1]);

but it gives undefined. If I do a split by a single backslash, it gives output as \boy which is incorrect. I don't understand why the split doesn't work. It's still treating \b as a special character even after adding the extra \ after character 'l' in the string.

nehacharya
  • 925
  • 1
  • 11
  • 31
  • Unfortunately, as soon as JS gets the strings it turns `\b` into a special character - I think you'd need a label function. (can't remember off the top of my head) – Jack Bashford May 07 '20 at 08:05
  • *so the solution to add the extra backslash manually for every string is not feasible.* Not really, it's pretty trivial to escape all characters that need escaping – CertainPerformance May 07 '20 at 08:06
  • @CertainPerformance These strings are coming from a database and there are thousands of them so escaping every string will be time consuming – nehacharya May 07 '20 at 08:09
  • 2
    If your *actual code* is like `var str = "school\boy";` then you don't have any actual backslashes in the code, but if the data is being taken from, eg, a network request, if it has backslashes, it's formatted correctly already..? – CertainPerformance May 07 '20 at 08:13
  • 1
    " It's still treating \b as a special character even after adding the extra \ after character 'l' in the string." — The \ only exists in the source code. Adding a \ to the in-memory string doesn't change the source code from which the string was created in the past to stop the \ being treated as an escape. – Quentin May 07 '20 at 08:16
  • what is your expected output? – StepUp May 07 '20 at 08:25
  • @CertainPerformance yes the actual code is `var str = "school\boy";` and you're probably right about the backslash not being there because if I print "\b", it's giving a weird character. I guess we see it as a \ but the computer doesn't understand that? – nehacharya May 07 '20 at 08:26
  • 1
    If that's the actual code, then whatever's generating the code is broken and should be fixed. Since this is from a database, it sounds like it's being generated dynamically, right? – CertainPerformance May 07 '20 at 08:27
  • @CertainPerformance yes you're right. The above code works for all characters after the \ which don't have any meaning. But in case of characters like \b or \n or \t etc., this code doesn't work and I am unable to extract the substring. So you're suggesting that a fix needs to be made at the back-end then? – nehacharya May 07 '20 at 08:31
  • 2
    Is the code being generated dynamically? If so, that code generator is broken, and should be fixed. (though, ideally, you wouldn't be *writing* dynamic JS like that anyway, better to use something like JSON) – CertainPerformance May 07 '20 at 08:33
  • @CertainPerformance yes the code's generated dynamically. – nehacharya May 07 '20 at 08:37

1 Answers1

1

I'm assuming your back-end is generating the JavaScript code (with only one backslash). The best thing to do is to reconfigure your back-end.

You can also use fetch() to perform a GET request to obtain the string, which won't need escaping.

However, if for some reason, you can't do that, you can use String.raw():

const str = String.raw `school\boy`; // Yes, without parenthesis.
console.log(str.split("\\")[1])

But if you use String.raw(), make sure there's something after the backslhash, as \` will not terminate the string!

D. Pardal
  • 6,173
  • 1
  • 17
  • 37