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.

Share Improve this question edited Jan 11, 2014 at 15:27 nhahtdh 56.8k15 gold badges129 silver badges164 bronze badges asked Oct 9, 2013 at 1:04 AlexAlex 7,68825 gold badges86 silver badges153 bronze badges 12
  • @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 and closed 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
 |  Show 7 more ments

3 Answers 3

Reset to default 4

Use 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