admin管理员组文章数量:1410696
I have a string like this :
whatever/aaazzzzz
and sometimes a string like that :
whatever\bbbbzzzzz
I would like to split the string when I match /
or \
The regex I tried seems to work
When I use it in the fiddle, it works with /
but not with \
Any ideas?
I have a string like this :
whatever/aaazzzzz
and sometimes a string like that :
whatever\bbbbzzzzz
I would like to split the string when I match /
or \
The regex I tried seems to work
https://regex101./r/gP5gL0/1
When I use it in the fiddle, it works with /
but not with \
Any ideas?
Share Improve this question asked Feb 1, 2016 at 16:56 DeseaseSpartaDeseaseSparta 2712 gold badges4 silver badges12 bronze badges 4-
2
It should be
test = "whatever\\aaaaaa"
(two slashes). – georg Commented Feb 1, 2016 at 16:59 - 1 Add you actual code, so we can se why isn't working – Jorge Campos Commented Feb 1, 2016 at 16:59
- 1 Seems to work: jsfiddle/vjxtwt2m – sp00m Commented Feb 1, 2016 at 16:59
-
What if I only have
aa\bb
and notaa\\bb
– DeseaseSparta Commented Feb 1, 2016 at 17:00
2 Answers
Reset to default 3The issue here is not the regex itself, but the unavoidable fact that JavaScript doesn't implicitly support string literals (i.e. ones where backslashes are interpreted as printed as opposed to denoting an escape sequence. Much more can read here).
Strings derived from any source other than source code are interpreted as literal by default, as demonstrated in this fiddle.
<script>
function splitTheString()
{
//test = escape("whatever\aaaaaa");
var test = document.getElementById("strToSplit").value;
a = test.split(/(\\|\/)/)[0];
alert(a);
}
</script>
<form>
Split this string:<br>
<input type="text" id="strToSplit">
<a href="javascript:splitTheString();">Split the string</a>
</form>
Use this
var arr = str.split(/[\\|\/]/);
var str = 'whatever\\bbbbzzzzz';
alert(str.split(/[\\|\/]/))
本文标签: javascriptRegex to match forward or backward slashStack Overflow
版权声明:本文标题:javascript - Regex to match forward or backward slash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745048931a2639533.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论