简体   繁体   中英

Split Javascript string containing single backslash to two strings

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?

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 mystring = 'TEST NAME/TEST ADDRESS';
var splittable = mystring.split('/');
string1 = splittable[0];
string2 = splittable[1];

 var str = "TEST NAME\\\\TEST ADDRESS"; // Show: TEST NAME\\TEST ADDRESS console.log(str); var res = str.split("\\\\"); console.log(res);

For Backslash or For URL or For Path:

If Suppose, we are getting path like below:

C:\fakepath\yourImage.jpeg

If you want to take filename alone,

var scanImagePath = C:\fakepath\yourImage.jpeg;
choosedFileName = scanImagePath.substring(scanImagePath.lastIndexOf("\\") + 1, scanImagePath.length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM