Javascript Regex multiple group matches in pattern

Posted by mummybot on Stack Overflow See other posts from Stack Overflow or by mummybot
Published on 2010-05-18T15:48:05Z Indexed on 2010/05/18 15:50 UTC
Read the original article Hit count: 219

Filed under:
|
|

I have a question for a Javascript regex ninja: How could I simplify my variable creation from a string using regex? I currently have it working without using any grouping, but I would love to see a better way!

The string is:

var url = 'resources/css/main.css?detect=#information{width:300px;}';

The code that works is:

var styleStr = /[^=]+$/.exec(url).toString();
var id = /[^\#][^\{]+/.exec(styleStr).toString();
var property = /[^\{]+/.exec(/[^\#\w][^\:]+/.exec(styleStr)).toString();
var value = /[^\:]+/.exec(/[^\#\w\{][^\:]+[^\;\}]/.exec(styleStr)).toString();

This gives:

alert(id)       //information
alert(property) //width
alert(value)    //300px

Any takers?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex