i want to split "A//B/C" string using javascript split()
function
"A//B/C".split(/\//g)
but it's output is ["A", "", "B", "C"]
but my expected output is
["A/", "B", "C"]
how i do this using javascript ?
我更新了@Tushar答案,并尝试了它,这对我来说是可行的..增加了\\b
以仅匹配正斜杠后跟单词边界,例如[az]和[0-9]
"A//B/C".split(/\/\b/)
尝试RegExp
/\\/(?=[AZ]|$)/
以匹配/
如果后跟AZ
或输入结尾)
"A//B/C".split(/\/(?=[A-Z]|$)/)
You need to split the string at a position, where the current character is "/" and the following character is not "/". However, the second (negative) condition should not be consumed by the regex. In other words: It shouldn't be regarded as delimeter. For this you can use a so called "look ahead". There is a positive "look ahead" and a negative one. Here we need a negative one, since we want to express "not followed by". The syntax is: (?!<string>)
, whereas is what shouldn't 'follow'.
So there you go: /\\/(?!\\/)/
applied to your example:
"A//B/C".split(/\/(?!\/)/); // ["A/", "B", "C"]
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.