Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Regular Expressions in Javascript

Hungover and can't cope with it...



10:37 am on Jan 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

I am a little hungover this morning and my bruised brain isn't coping well... I need a function that can take a string of XML and update a specified node's value that is a member of specified parent node (identified by one of it's siblings). I got so far:

var re;

var parentnode = "buildset"
var newnode = "number"
var newvalue = "30"
var wherenode = "setobject"
var wherevalue = "SetPerson"

var sWhereNode = "<" + wherenode + ">" + wherevalue + "<\\/" + wherenode + ">";
var openParentNode = "<" + parentnode + ">";
var endParentNode = "<\\/" + parentnode + ">";
var openNewNode = "<" + newnode + ">";
var endNewNode = "<\\/" + newnode + ">";

var normString = "(" + openParentNode + ".*" + sWhereNode + ".*" + openNewNode + ").*(" + endNewNode + ".*" + endParentNode + ")"

re = new RegExp(normString);

//You'll need to unwrap this long string
xml = "<buildsets><setsname>View</setsname><buildset>

xml = xml.replace(re, "$1" + newvalue + "$2");



In the example given above I want to change the <number> node (newnode) to 30 (newvalue) where the parent or group node is <buildset> (parentnode). However I only want to update the <number> node in the <buildset> group that has a node called <setobject> with a value of SetPerson. Follow??

The match comes out but it always inserts the update in the last parentnode group. Grrr! At present it's OK to assume that the newnode appears after the wherenode in the parent group.

Please help :(


11:45 am on Jan 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

OK, simpler question...

In RegExp how can I test that a string does NOT contain a string without switching the boolean after output?


testString = "thatstringdoesntappearhere"

I would want the test to return true if the string DIDN'T contain, say, "josh".

I've tried

re = /(josh){0}/
re = /[^(josh)]/

each without success.


Featured Threads

Hot Threads This Week

Hot Threads This Month