Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 28th, 2008, 06:10 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default REG EXPRESSION

Hi,

I am struggling to write a regular expression that would extract the
survey ID from a string and would appreciate if someone could help me to fix it.

I need to extract the "span_spr08_arg" text and assign it to a variable.

payload = '<SurveyReference>span_spr08_arg</SurveyReference>'

That is what I have tried but to no avail:

SurveyID = payload.replace(/<SurveyReference>([^<]+)/,"$1")

but this is what I am getting : span_spr08_arg</SurveyReference>

Please note that I could load the string into a DOM object and extract the id but I do not want to use the DOM.

Cheers

Claudio
 
Old August 28th, 2008, 06:21 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well you didn't match the end tag so your replace is working as it should, just replacing what you found. Try:
Code:
SurveyID = payload.replace(/<SurveyReference>([^<]+)<\/SurveyReference>/,"$1")
(You'll have to check if \/ is the correct way to escape a / character within a regex.)

I think there are a few simpler ways of finding the ID though.

--

Joe (Microsoft MVP - XML)
 
Old August 28th, 2008, 06:56 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Joe,

Thanks for the reply.

I had to make some changes for that to work with the whole string:

SurveyID = payload.replace(/[^~]+<SurveyReference>([\w]+)<\/SurveyReference>[^~]+/g,"$1")

This seems to be working now but it looks ugly.

payload = "<xmlParams>
  <RecordLabel>
    <SearchString>car</SearchString>
    <SurveyReference>span_spr08_arg</SurveyReference>
    <RecordID>S17776524</RecordID>
    <RecordType>A</RecordType>
<UserCookie>E51364468F23529BCEB8147876487E289B9275 C7866924BC57D8257D4595BB1C37859290FE76D74CD82A9055 03CE19D8AFC11644290B36E4BBAA1C51631F2A54D1B5718B20 38696EDE496136DB3D57E9A1C3848FF22AF8F327075D0CDD85 0345BB44DC8B6A43CAD30604F8DD5A4477E0</UserCookie>
</RecordLabel>
</xmlParams>"
 
Old August 28th, 2008, 07:16 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You should have said that wasn't the whole text. You can use .+ (one or more of anything) instead of [^~]+ (anything except a tilde).

--

Joe (Microsoft MVP - XML)
 
Old August 28th, 2008, 09:37 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Thanks




Similar Threads
Thread Thread Starter Forum Replies Last Post
Message> in query expression <expression>. (Error ybg1 Access 5 July 15th, 2007 05:42 AM
reg ex help swarnap Javascript How-To 1 January 9th, 2007 02:14 AM
reg ex help swarnap Javascript 1 January 8th, 2007 08:24 AM
Parse Txt File with Reg Expression harpua Classic ASP Basics 2 June 9th, 2004 12:42 AM
Wrox URL reg. expression richard.york Forum and Wrox.com Feedback 0 May 20th, 2004 12:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.