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.