简体   繁体   中英

How to split string with character \ - Javascript

I want to split a string into an array of letters, but I have a problem with the special character \ .

The code:

let s = '\op*Bw'.split('');

But I got this result ['o', 'p', '*', 'B', 'w'] , that is not correct because I want the \ character too in the array.

Backslashes '\' escape the next character in the sequence. If you need the '\' in the string, your string will need to look like '\\op*Bw'

let s = '\\op*Bw'.split('');
console.log(s) // ['\', 'o', 'p', '*', 'B', 'w']

You can also use String.raw

String.raw`\op*Bw`.split('')

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