homepage Welcome to WebmasterWorld Guest from 54.234.128.25
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Regular Expressions in Javascript
Hungover and can't cope with it...
joshie76




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

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




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved