在Javascript中拆分字符串但保留分隔符/ [英] Split String in Javascript but keep delimiter /
本文介绍了在Javascript中拆分字符串但保留分隔符/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var string = 'Animation/rawr/javascript.js'
//expected output
// ['Animation/', 'rawr/', 'javascript.js']
我无法正确拆分此字符串.我可以得到一些帮助吗?
I'm having trouble splitting this string properly. Can I get some help on this?
string.split(/(/)/)
string.split(/(/)/)
推荐答案
您可以使用 ''.match()
而不是 split
:
var str = 'Animation/rawr/javascript.js';
var tokens = str.match(/[^/]+/?|//g);
第一部分 [^/]+/?
匹配尽可能多的非正斜杠,它可以选择后跟 /
.第二部分 /
(在 or 之后:|
)匹配一个单独的正斜杠.
The first part [^/]+/?
matches as many non forward slashes it can optionally followed by a /
.
The second part /
(after the or: |
) matches a lone forward slash.
这篇关于在Javascript中拆分字符串但保留分隔符/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文