admin管理员组文章数量:1322838
I'm trying to match characters before and after a symbol, in a string.
string: budgets-closed
To match the characters before the sign -
, I do: ^[a-z]+
And to match the other characters, I try: \-(\w+)
but, the problem is that my result is: -closed
instead of closed
.
Any ideas, how to fix it?
Update
This is the piece of code, where I was trying to apply the regex /
I repeat: It's not that I don't want to use split; it's just I was really curious, and wanted to see, how can it be done the regex way. Hacking into things spirit
Update2
Well, using substring
is a solution as well: / and is the one I chosed to use, since the if in question, is actually an else if
in a more plex if
syntax, and the chosen solutions seems to be the most fitted for now.
I'm trying to match characters before and after a symbol, in a string.
string: budgets-closed
To match the characters before the sign -
, I do: ^[a-z]+
And to match the other characters, I try: \-(\w+)
but, the problem is that my result is: -closed
instead of closed
.
Any ideas, how to fix it?
Update
This is the piece of code, where I was trying to apply the regex http://jsfiddle/trDFh/1/
I repeat: It's not that I don't want to use split; it's just I was really curious, and wanted to see, how can it be done the regex way. Hacking into things spirit
Update2
Well, using substring
is a solution as well: http://jsfiddle/trDFh/2/ and is the one I chosed to use, since the if in question, is actually an else if
in a more plex if
syntax, and the chosen solutions seems to be the most fitted for now.
- @Floris testing it with regexpal, and it doesn't work – Alex Commented Oct 9, 2013 at 1:09
- 2 There is no lookbehind support in JS. – elclanrs Commented Oct 9, 2013 at 1:09
- When you say "trying to match characters" - what would you like the output to be? – Floris Commented Oct 9, 2013 at 1:14
-
@floris well... I'm trying to have
budgets
andclosed
without using split. – Alex Commented Oct 9, 2013 at 1:17 - 1 "trying to have" - in separate variables? in the same variable? Could you write a short piece of code that has "and here a miracle happens" as one of the steps? Some of the solutions proposed seem quite legitimate but you don't like them - trying to figure out why not. – Floris Commented Oct 9, 2013 at 1:18
3 Answers
Reset to default 4Use exec():
var result=/([^-]+)-([^-]+)/.exec(string);
result
is an array, with result[1]
being the first captured string and result[2]
being the second captured string.
Live demo: http://jsfiddle/Pqntk/
I think you'll have to match that. You can use grouping to get what you need, though.
var str = 'budgets-closed';
var matches = str.match( /([a-z]+)-([a-z]+)/ );
var before = matches[1];
var after = matches[2];
For that specific string, you could also use
var str = 'budgets-closed';
var before = str.match( /^\b[a-z]+/ )[0];
var after = str.match( /\b[a-z]+$/ )[0];
I'm sure there are better ways, but the above methods do work.
If the symbol is specifically -
, then this should work:
\b([^-]+)-([^-]+)\b
You match a boundry, any "not -
" characters, a -
and then more "not -
" characters until the next word boundry.
Also, there is no need to escape a hyphen, it only holds special properties when between two other characters inside a character class.
edit: And here is a jsfiddle that demonstrates it does work.
本文标签: javascriptRegex trying to match characters before and after symbolStack Overflow
版权声明:本文标题:javascript - Regex trying to match characters before and after symbol - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742117223a2421524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论