0

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.net/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.net/trDFh/2/ and is the one I chosed to use, since the if in question, is actually an else if in a more complex if syntax, and the chosen solutions seems to be the most fitted for now.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Alex
  • 7,538
  • 23
  • 84
  • 152
  • @Floris testing it with regexpal, and it doesn't work – Alex Oct 09 '13 at 01:09
  • 2
    There is no lookbehind support in JS. – elclanrs Oct 09 '13 at 01:09
  • When you say "trying to match characters" - what would you like the output to be? – Floris Oct 09 '13 at 01:14
  • @floris well... I'm trying to have `budgets` and `closed` without using split. – Alex Oct 09 '13 at 01:17
  • I gave you a solution for that type of string, without regexp grouping or split. Is it what you need? – Joe Simmons Oct 09 '13 at 01:18
  • 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 Oct 09 '13 at 01:18
  • I've posted a link to a jsfiddle. Thank you folks for the help! I posted this question out of the curiosity of learning and see if someone could bring a solution to my poor regex language knowledge. I guess I will use `split` – Alex Oct 09 '13 at 01:25
  • @w0rldart your update 2 kind of contradicts your first update... – Christophe Oct 09 '13 at 01:37
  • Here's how you could do it without regex at all: http://jsfiddle.net/thetenfold/trDFh/3/ – Joe Simmons Oct 09 '13 at 01:40
  • source = '/panel/' + remote.match(/^[a-z]+(?=\-)/g) + '/data/' + remote.match(/(?!\-)(\w+)$/g); – Dart Oct 09 '13 at 01:42
  • @JoeSimmons interesting, but I'd rather use `.replace` and convert `budgets-closed` to `budgets/closed`. – Alex Oct 09 '13 at 02:00
  • Hey, it's your code. Do what you need :) – Joe Simmons Oct 09 '13 at 02:38

3 Answers3

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.net/Pqntk/

Christophe
  • 27,383
  • 28
  • 97
  • 140
2

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.

Joe Simmons
  • 1,828
  • 2
  • 12
  • 9
0

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.

Community
  • 1
  • 1
  • well, the thing is that your proposed solution... it doesn't work. I gave it a quick try using regexpal – Alex Oct 09 '13 at 01:14
  • Oh, I see.. was testing it in a different way. – Alex Oct 09 '13 at 01:18
  • @w0rldart regexpal matches the entire pattern, and doesn't show you the grouping within the regex. I'd recommend [rubular](http://rubular.com/) instead. (Fun fact, almost all regexes in all languages use the same PREL syntax). –  Oct 09 '13 at 01:20
  • Yea, I had figure it out after testing at regexpal, that your regex matches the whole string. Nice touch – Alex Oct 09 '13 at 01:26