2

I have been banging my head all day trying to figure out how to formulate what seems to be a tricky regex expression. Here we go:

NAME=JOHN,SMITH

I want to select "JOHN" and "SMITH" (not the comma nor the equals sign)

In other words, I want an expression to select LETTERS after an equals sign (while not including the equals sign itself) regardless of whether or not the letters/words are separated by a comma. I'm afraid I can't change the formatting.

Thanks. I'm using JavaScript btw.


EDIT: Here is what I managed to put together so far. /=(\D[^=\n,]+)/g However, this still selects the equals sign and doesn't add any other words that may come after a comma.

15
  • What have you come up with so far? Commented Jun 17, 2015 at 16:07
  • regardless of whether or not the letters/words are separated by a comma. This means this example you would want the result be 'JOHN,SMITH' ? Commented Jun 17, 2015 at 16:08
  • @fuyushimoya no the name could be just "JANE" or 'JOHN" and "SMITH" (either just a first name or a first and last name seperated by just a comma) Commented Jun 17, 2015 at 16:10
  • don't overthink it: /NAME=(\w+),?(\w+)?/ Commented Jun 17, 2015 at 16:11
  • 1
    sigh.. please post actual "before" and "after" examples for what you actually want. e.g. what do you want a result variable to actually contain. show an actual example or two Commented Jun 17, 2015 at 16:17

4 Answers 4

3

I want to select "JOHN" and "SMITH"

Use this :

'NAME=JOHN,SMITH'.match(/\=(\w+),?(\w+)/)

the result will be on the second and third index of the array :

["=JOHN,SMITH", "JOHN", "SMITH"]

Sign up to request clarification or add additional context in comments.

7 Comments

I absolutely agree with you except that I am also using a plugin (probably should have mentioned that above) that accepts nothing but a regex expression. (I couldn't use .match() ) if I wanted to. So frustrating!
The regex is nothing but \=(\w+),?(\w+)
@Walendas - Then please post your real requirements.
@Walendas What does your plugin except as a parameter ?
@RoyiNamir nothing but a regex expression
|
0

You can do something like this:

/\w+(?!.*=)/g

That will do a negative lookahead meaning you want a word: \w+ that is NOT before any number of characters plus an = sign.

Then the global will give you multiple matches excluding a comma. I believe this is what you're looking for? You won't have any match groups, but I don't believe you need any from what you've said so far.

Regex101 Example

Comments

0

Will this do?

var str = 'NAME=JOHN,SMITH';
str = str.replace(/NAME\=([A-Z]+?)\,([A-Z]+?)/g,'$1 $2');
alert(str);

http://jsfiddle.net/t5ed7gcr/1/

Comments

0

This regex should do it for you

/(.+?(?==))|([^,?=?]+)/g

The first part, (.+?(?==)), uses a positive lookahead to search for the = character. It basically groups anything before the = character

The second part, ([^,?=?]+) groups everything that isn't a , or a =, so it groups the other two components of your search string. Tell me if this works for you :)

If you do not want the NAME part, you can use simply /(\w+(?!.*=))/g.

If you do not want it to be grouped, you can remove the parentheses which gives you /.+?(?==)|[^,?=?]+/g and /\w+(?!.*=)/g.

You can test the regex here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.