Welcome to WebmasterWorld Guest from 54.234.129.215

Forum Moderators: open

Message Too Old, No Replies

Regular Expressions in Javascript

Hungover and can't cope with it...

     

joshie76

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:

<script>
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>
<setobject>SetPerson</setobject><setname>Person</setname>
<number>0</number><selected>true</selected></buildset>
<buildset><setobject>SetPosition</setobject><setname>Position</setname>
<number>0</number><selected>false</selected></buildset><buildset>
<setobject>SetJob</setobject><setname>Job</setname><number>0</number>
<selected>false</selected></buildset><buildset>
<setobject>SetOrganisationalUnit</setobject>
<setname>OrganisationalUnit</setname>
<number>0</number><selected>false</selected>
</buildset><buildsets>";

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

alert(xml);

</script>

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 :(

joshie76

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?

eg.

testString = "thatstringdoesntappearhere"

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

I've tried

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

each without success.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month