Consider this below string in JavaScript:
"TEST NAME\TEST ADDRESS"
(it contains only one "\" which cannot be changed).
Now, this above string needs to be split into two string by "\" char.
Resulting strings:
"TEST NAME"
"TEST ADDRESS"
How, can this be done in JavaScript?
split() method to split a string on the backslashes, e.g. my_list = my_str. split('\\') . The str. split method will split the string on each occurrence of a backslash and will return a list containing the results.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
Use the String. replaceAll() method to remove all backslashes from a string, e.g. str. replaceAll('\\', '') . The replaceAll() method will remove all backslashes from the string by replacing them with empty strings.
Do like this:
var str = "TEST NAME/TEST ADDRESS";
var res = str.split("/");
You will get first part on res[0] and second part on res[1].
var str = "TEST NAME\\TEST ADDRESS"; // Show: TEST NAME\TEST ADDRESS
console.log(str);
var res = str.split("\\");
console.log(res);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With