Re: 2 Qs re JS scripting
- From: "Trevor L." <tandcl@xxxxxxxxxxxxxxx>
- Date: Mon, 15 Aug 2005 11:16:16 +1000
Dave,
Thank you for this little tutorial. I am trying to learn more about regular
expressions so every bit helps
I think I am correct in saying
str.replace(/%20|\+/g," "))
translates in English as
Replace every occurrence in str of either %20 or + with blank
No need to reply if I am right
:-))
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au
Dave Anderson wrote:
> Trevor L. wrote:
>> function qsobj(parm)
>> {
>> var qstring = document.location.search.substring(1)
>> var qpairs = qstring.split("&")
>> var qvbl = qpairs[parm].split("=")
>> return unescape(qvbl[1].replace("%20"," ").replace("+"," "))
>> }
>> I found this on the web somewhere and refined it a little.
>> If any one can make it more succinct, would be grateful
>
> If the first argument of String.replace() is a string, only the first
> match will be replaced. If you use a regular expression, you can make
> it replace all instances:
>
> String.replace(/%20|\+/g," ")
>
> Now, you may have been fooled in the past into thinking you got a
> global replacement on %20. But Internet Explorer treats that value as
> " ". If you examine the problem in ASP, however, you will see how
> *JScript* handles it:
> <%@ Language=JScript %><%
> var str = "this+is+a%20test%20sentence"
> Response.Write(str.replace("%20"," ").replace("+"," "))
> %>
>
> The output?
>
> this is+a test%20sentence
>
> Using the regular expression:
>
> Response.Write(str.replace(/%20|\+/g," "))
>
> Result: this is a test sentence
>
>
>
>> The replace of %20 and + may not be necessary in all
>> cases. I understood that some browsers use + in place
>> of blanks
>
> I believe the + always results from form submission (with method
> GET), while %20 is generated by the URI escaping mechanism. Most
> typically, the mechanism is used for HREF attributes and when typing
> an address into the browser's location bar.
>
>
> --
> Dave Anderson
>
> Unsolicited commercial email will be read at a cost of $500 per
> message. Use of this email address implies consent to these terms.
> Please do not contact me directly or ask me to contact you directly
> for assistance. If your question is worth asking, it's worth posting.
.
- References:
- 2 Qs re JS scripting
- From: Adrian
- Re: 2 Qs re JS scripting
- From: Bob Barrows [MVP]
- Re: 2 Qs re JS scripting
- From: Randy Webb
- Re: 2 Qs re JS scripting
- From: Trevor L.
- Re: 2 Qs re JS scripting
- From: Dave Anderson
- 2 Qs re JS scripting
- Prev by Date: Re: 2 Qs re JS scripting
- Next by Date: Re: 2 Qs re JS scripting
- Previous by thread: Re: 2 Qs re JS scripting
- Next by thread: Re: 2 Qs re JS scripting
- Index(es):
Relevant Pages
|