admin管理员组文章数量:1387360
'{5}<blah>{0}</blah>'
i want to turn that into:
['{5}', '<blah>', '{0}', '</blah>']
i currently use: ________.split(/({.*?})/);
but this fails when curly brace is the first character as in the case:
'{0}<blah>'
which gets turned into:
['', '{0}', '<blah>']
... a 3 element array, not a 2
what's wrong with my regex?
Thanks!
'{5}<blah>{0}</blah>'
i want to turn that into:
['{5}', '<blah>', '{0}', '</blah>']
i currently use: ________.split(/({.*?})/);
but this fails when curly brace is the first character as in the case:
'{0}<blah>'
which gets turned into:
['', '{0}', '<blah>']
... a 3 element array, not a 2
what's wrong with my regex?
Thanks!
Share Improve this question edited Sep 10, 2009 at 16:54 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Sep 10, 2009 at 13:18 rawrrrrrrrrrawrrrrrrrr 3,6977 gold badges29 silver badges33 bronze badges 1- Try removing the parenthesis, and making this a one-or-more match. For example, /{.+?}/. – David Andres Commented Sep 10, 2009 at 13:26
3 Answers
Reset to default 5There's nothing wrong with your regex, but there's an issue with how you're using split. Split returns an array based on a delimiter, so if the delimiter is FIRST, it gives you the stuff to the left and right of the split item.
Just check to see if the first item == '' and remove it if it is.
This should do it:
split(/((?!^)\{.*?\})/)
The negative lookahead -- (?!^)
-- succeeds iff the match does not start at the beginning of the string.
What do you think of:
'{5}<blah>{0}</blah>'.split(/{([^}]+)}/g)
The value of the curly blocks are every 2 items from the item 1.
本文标签: javascriptSplitting a string with curly braces as delimetersStack Overflow
版权声明:本文标题:javascript - Splitting a string with curly braces as delimeters? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744570112a2613286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论