(e-mail address removed) (Kelmen Wong) wrote in message
<html>
<head>
<title>none-greedy RegExp-pattern</title>
<script type="text/javascript">
<!--
/*
hi Kelmen,
eg.
[test1] = [test2]
- return array [test1] or test1, [test2] or test2
I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!
thats due to the greedy nature of RegExp-patterns;
and "greedy" means:
a pattern alway tries to take as much chracters of
a given string as long as the search rule will match;
in your example "[test1] = [test2]" the last possible
character that will fit with the RegExp /\[(.*)\]/;
is the closing square-bracket of "...test2]" because
of ".*" that allowes as much characters as possible
between an opening and a closing square-bracket;
none-greedy patterns will solve your problem;
you might play with the following lines:
*/
var givenString = "test1 [test2] test3] [test4] [test5] [test6
[test7 test8]";
var regExpression = /\[[^\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is not a closing square bracket -
[^\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
var matches = [];
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
regExpression = /\[[^\[\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is neither a opening nor a closing
square bracket - [^\[\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
givenString = "[test1] = [test2]";
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
// if you like to free your matches from its enclosing brackets
treat your "matches"-array as shown right now:
//
// matches = matches.join("splitnjoin");alert(matches);
// matches = matches.replace(/\[/g,"");alert(matches);
// matches = matches.replace(/\]/g,"");alert(matches);
// matches = matches.split("splitnjoin");alert(matches);
//
matches = matches.join("splitnjoin").replace(/\[/g,"").replace(/\]/g,"").split("splitnjoin");
alert("free from enclosing brackets\n\nmatches.join(\"\\n\") :\n"
+ matches.join("\n"));
// NOTE:
//
// in javascript 1.5 none-greedy search patterns can easly
// be written by using the punctuation character "?";
//
// /\[[^\]]*\]/g; then will be /\[.*?\]/g;
/*
givenString = "test1 [test2] test3] [test4] [test5] [test6 [test7
test8]";
regExpression = /\[.*?\]/g; // was: /\[[^\]]*\]/g;
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
// /\[[^\[\]]*\]/g; will turn into /\[[^\[]*?\]/g;
regExpression = /\[[^\[]*?\]/g; // was: /\[[^\[\]]*\]/g;
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
*/
//
// have fun - regards - peterS. - (e-mail address removed)
//
//-->
</script>
<head>
<body>
</body>
</html>