Mostafa Talebi
Mostafa Talebi

Reputation: 9183

Javascript regular expression to capture characters

I have written the following regexp in Javascript:

var rule = /^<[a-zA-Z0-9].*>/;

And the I checked it against this string:

var str = "<string stringValue><string2 stringValue>";

And the I executed:

var res = rule.exec(str);

And the res retruns:

<string stringValue> <string2 stringValue2>

Everything works in the way I need. But I must change two things: 1- First capture each occurrence (i mean each block of tag, in my example there are two)

2- I should strip off the tags [<>] in returned value. Is it then possible?

Upvotes: 2

Views: 99

Answers (3)

fulme
fulme

Reputation: 332

Try like this:

var reg = /<[a-zA-Z0-9][^>]*>/g;
var str = "<string stringValue><string2 stringValue>";
var res = str.match(reg);
res = res ? res.map(function(i){return i.replace(/^<|>$/g, '')}) : res;
// res[0]: string value: stringValu
// res[1]: string2 value: stringValue

Good luck!

Upvotes: 0

Nevett
Nevett

Reputation: 996

Regular expressions are, by default, "greedy". The .* in your rule will match as many characters as possible while still succeeding. Also, since you used ^ at the start, your pattern would only match the tag at the start of your input. This is why you are matching too much currently.

/<([^>]*)>/

Will match either tag, and put the contents (without the < >) in a capture group.

To find every tag, you can run the regular expression multiple times. Adding /g to the end makes the expression global, which allows for this behaviour.

var rule = /<([^>]*)>/g,
    match,
    tags = [],
    input = "<string stringValue><string2 stringValue>";

while (match = rule.exec(input)) {
    tags.push(match[1]);
}

console.log(tags);

In each loop, match[1] refers to the first capture group, the parentheses in the expression.

It will return:

[ "string stringValue", "string2 stringValue" ]

Upvotes: 4

xdazz
xdazz

Reputation: 160843

I guess you want to get the key value pair. Try the code below:

var rule = /<([a-zA-Z0-9]*)\s+([a-zA-Z0-9]*)>/g;
var str = "<string stringValue><string2 stringValue>";
var res;

while((res = rule.exec(str)) !== null) {
    console.log("key: "+res[1]+" value: "+res[2]);
}

//output
//key: string value: stringValue
//key: string2 value: stringValue 

Upvotes: 4

Related Questions