PDA

View Full Version : js RegExp: escaping parentheses


n8thegreat
08-05-2003, 10:34 PM
i am making a function that parses the clip property of an element, since IE will only store the string value. say the string is "rect(0px 100px 100px 0px)" and i want to get rid of the rect( and ), but how can you escape parentheses so it will replace the literal ( and ). ive tried using \( and \) but it doesnt work, does anyone know how to do this?

agent002
08-06-2003, 04:18 AM
<script language="JavaScript" type="text/javascript"><!--

var coords = 'rect(0px 100px 100px 0px)';
var parsed = coords.substring((coords.indexOf('(')+1),coords.lastIndexOf(')'));

//--></script>

The value of "parsed" should be the coords without rect and the parentheses.

n8thegreat
08-06-2003, 11:16 AM
thanks! i should learn more actual javascript functions, all i know is dhtml :P

anyways, is anyone want the code

function parseClip( coords) {
parsed = coords.substring((coords.indexOf('(')+1),coords.lastIndexOf(')'));
parsed = parsed.split(" ");
for ( num = 0; num < parsed.length; num++ ) {
parsed[ num ] = parseInt( parsed[ num ] );
}
parsed.top = parsed[ 0 ];
parsed.right = parsed[ 1 ];
parsed.bottom = parsed[ 2 ];
parsed.left = parsed[ 3 ];
return parsed;
}

you can either acces them like clip.top, etc, or clip[0]