1

Im trying to match the following format: < any number of a-z A-Z characters >

Using "^<\w*>$";

The code is:

var predefinedListRegEx = "^<\w*>$";
var dataFill = "<aaaa>"; 
var predefined_List = dataFill.match(predefinedListRegEx);

if (predefined_List != null) {
        //MATCHES THE CONDITION
    }

Cant seem to get it to work.. Where am i going wrong?

Also once i get the matched string i woud like to subtract whats been the <> out And use it to reference a varible.

var vacba = 0 

e.g

And then to vacba = 10;

Christoph
  • 50,121
  • 21
  • 99
  • 128
Lemex
  • 3,772
  • 14
  • 53
  • 87
  • `console.log(typeof predefinedListRegEx === "string");` – ChaosPandion Aug 28 '12 at 13:23
  • 1
    Your request to access global variable by name is actually another question, that shouldn't be mixed with first one. Anyway, it is pretty common and answered in several other entries, for example here: http://stackoverflow.com/questions/4399794/access-value-of-javascript-variable-by-name. It is bad practice, though, and you should use some other, non-global object in such cases instead. – Oleg V. Volkov Aug 28 '12 at 13:34

3 Answers3

4

Your regex here is a String, not a RegExp. Try:

var predefinedListRegEx = /^<\w*>$/;

If for some reason you need to use a string that is cast to a regex by match, you'd have to escape your slash:

var predefinedListRegEx = "^<\\w*>$";

In response to your edit with more requests:

Use a parenthesized match group:

var predefinedListRegEx = /^<(\w*)>$/;
var dataFill = "<aaaa>"; 
var predefined_List = dataFill.match(predefinedListRegEx);

This will set predefinedListRegEx to an array like: ["<aaaa>", "aaaa"].

If you want to use the string in predefined_List[1] as a variable name (e.g., to do aaaa = 10), you possibly don't need to use eval. If the variable is global, you can simply use window[predefined_List[1]] because all global variables are properties of the window object. If it's not global (or if you just want to be a tidy JavaScript programmer and not overuse the global namespace), you're best off just using referencing properties on an object that holds your values like variablesNamedInMyRegexes[predefined_List[1]] = 10;.

apsillers
  • 112,806
  • 17
  • 235
  • 239
3

You use string as container for regexp data. It will be implicitly converted to real regexp when you use in match, but you need to correctly quote symbols that have special meaning in string literals - in this case \.

var predefinedListRegEx = "^<\\w*>$";

Additionally, implicit conversion on each call imposes a performance decrease, so you really should be using real constructor, as mentioned by apsillers:

var predefinedListRegEx = /^<\w*>$/;
Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • 1
    +1 For explaining the root of the problem. It also makes more sense for them to know the difference between a string and RegExp object so there is less "magic". – ChaosPandion Aug 28 '12 at 13:29
1

Use the following regex:

result = dataFill.match(/<(\w*)>/);

This will return the match and the content of the capture-group as the second value.

Now you could use it as a variable name using the [] notation like the following:

window[result[1]] = "whatever";

this would create a global variable. Of course it is better to use your own namespace for that instead of the global object.

Christoph
  • 50,121
  • 21
  • 99
  • 128