Forum Moderators: open

Message Too Old, No Replies

javascript regular expression problem

Can not get # character in array element from RegExp.exec()

         

Moby_Dim

6:01 pm on Apr 12, 2005 (gmt 0)

10+ Year Member



There is a '#' character in a string, exec() does not work, test() only. How to force .exec()? Is it possible? Thanx.

Bernard Marx

9:34 pm on Apr 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What kind of tests aren't working for you?

Moby_Dim

4:19 am on Apr 13, 2005 (gmt 0)

10+ Year Member



Well, I'm parsing an array which is a copy of file.css. Each string represents an identifier, hence the first character of a string is '#'. A simple task for the first glance : /^(#)(.+)$/ to get '#' in the returned_array[1], but it does not work. Tried \# too, the same result. For .exec() and .match() both. The escape found is : to get 'true' using .test() for /^#/, and then return the whole string using .match() or .exec() - /(.+)/ In this case the result is a copy of the string WITHOUT '#' - like : css_id {property : value; property : value;....}, and it should be #css_id {.... So, '#' is missed. I do not like this solution, and want to know whether it is fatal. Thanx.

Bernard Marx

7:04 am on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm getting the same results for <string>.exec as for <reg>.match (with no g flag).
I hope I've got this right.

reg = /^(#)(.+)$/;
str = '#css_id {property1 : value1; property2 : value2;}';
alertAll( str.match(reg) );
alertAll( reg.exec(str) );

function alertAll(obj)
{
var out = '';
for(var p in obj)
out+= p+ ':\n\t '+obj[p]+'\n' ;
alert( out );
}

Moby_Dim

11:23 am on Apr 13, 2005 (gmt 0)

10+ Year Member



funny... it works. Need to peek my nose further - whether the array was parsed improperly. Thank you, Marx! (I was near to treat '#' as a 'dead' char for javascripting ;)