0

I have this string in a object:

<FLD>dsfgsdfgdsfg;NEW-7db5-32a8-c907-82cd82206788</FLD><FLD>dsfgsdfgsd;NEW-480e-e87c-75dc-d70cd731c664</FLD><FLD>dfsgsdfgdfsgfd;NEW-0aad-440a-629c-3e8f7eda4632</FLD>

this.model.get('value_long').match(/[<FLD>\w+;](NEW[-|\d|\w]+)[</FLD>]/g)

Returns:

[";NEW-7db5-32a8-c907-82cd82206788<", ";NEW-480e-e87c-75dc-d70cd731c664<", ";NEW-0aad-440a-629c-3e8f7eda4632<"]

What is wrong with my regular expression that it is picking up the preceding ; and trailing <

here is a link to the regex http://regexr.com?30k3m

Updated:

this is what I would like returned:

["NEW-7db5-32a8-c907-82cd82206788", "NEW-480e-e87c-75dc-d70cd731c664", "NEW-0aad-440a-629c-3e8f7eda4632"]

here is a JSfiddle for it

http://jsfiddle.net/mwagner72/HHMLK/

1 Answer 1

2

Square brackets create a character class, which you do not want here, try changing your regex to the following:

<FLD>\w+;(NEW[-\d\w]+)</FLD>

Since it looks like you want to grab the capture group from each match, you can use the following code to construct an array with the capture group in it:

var regex = /<FLD>\w+;(NEW[\-\d\w]+)<\/FLD>/g;
var match = regex.exec(string);
var matches = [];
while (match !== null) {
    matches.push(match[1]);
    match = regex.exec(string);
}

[<FLD>\w+;] would match one of the characters inside of the square brackets, when I think what you actually want to do is match all of those. Also for the other character class, [-|\d|\w], you can remove the | because it is already implied in a character class, | should only be used for alternation inside of a group.

Here is an updated link with the new regex: http://jsfiddle.net/RTkzx/1

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

3 Comments

And I believe he will want to grab the first capturing group. Seems to be trying to strip the beginning and end stuff off.
I appreciate your response, and I have looked at the link, however your regex is not matching the correct sections of the string.
@MarkWagner See my edit, I think it should now do what you want. I also included a JSfiddle link.

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.