0

I am trying to lift properties from a string like this:

var valueAccessor = "function (){return { id:'someId',label:'Some Label',value:PropertyOnModel} }";

This works when I specifically want to get value from the above string, specifically using this code:

var value = valueAccessor.match(/function(?=[^}]*value\s?:\s?(\w+))/im)[1]

I have a list of the different properties I want to lift, for example label and id so I iterate through them and dynamically set my regex using RegExp.

// Iterate through id, label, value
for(var prop in obj) {
 var propRegex = new RegExp('/function(?=[^}]*' + prop + '\\s?:\\s?(\\w+))/im');
 var propValue = valueAccessor.match(propRegex)[1];
}

Although the propRegex source is identical to the structure in the first example, it returns undefined and thus means I can't extract the relative values.

Can anybody explain the difference between the two and where I am going wrong?

2
  • 2
    new RegExp(pattern, flags); Commented Sep 3, 2014 at 15:06
  • 2
    The / are the delimiters of the regex literal, just like " are the delimiters of a string. They tell the parser where the pattern starts and ends. They are not part of the value (pattern) itself. If you don't use a regex literal, don't put / around the pattern. Commented Sep 3, 2014 at 15:09

2 Answers 2

3

It is

new RegExp(pattern, flags);

so you need to drop the / and move the im out

var propRegex = new RegExp('function(?=[^}]*' + prop + '\\s?:\\s?(\\w+))', 'im');

Reference: MDN RegExp constructor

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

4 Comments

Will accept the answer in a second, I haven't really used Regex to a high level in Javascript so I think I got a bit confused because I didn't specify any flags in the original string.match (even though there is an overload)
Felix Kling's comment has just kicked in... I understand now. When Stack lets me accept I will, cheers for that - had me caught for a while now! :P
The regex is also needlessly complicated, even though it works. There's no need for the lookahead: 'function[^}]*' + prop + '\\s?:\\s?(\\w+)'. Perhaps he meant to use a non-capturing group instead (?: ).
@BrianStephens I despise Regex! I spoke to one of our "regex guru's" in the office for this, I am sure he will be pleased to have your feedback ;)
0

It seems that your RegExp below is not well formed.

Try doing:

for(var prop in obj) {
 var propRegex = new RegExp("function(?=[^}]*" + prop + "\\s?:\\s?(\\w+))", "im");
 var propValue = valueAccessor.match(propRegex)[1];
}

3 Comments

That would literally look for + prop + inside the string, which is certainly not what the OP wants.
I forgot to change the single quotes inside the RegExp. I changed the first and last one (cause i prefer using double quotes) but forgot to change the others.
That's why it's always good to properly explain the problem and solution ;) Otherwise it's hard to tell what you changed to solve the problem and what might just be a typo.

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.