Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript 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 October 18th, 2007, 09:18 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default Regular Expression Help

Hi,

I am struggling to get this regular expression to work properly and would appreciate if someone could give some help. This what I have tried but to no avail:

/>[\s]\w*\s*/ , ""

I need to clear all the spaces (but only the blank spaces and not content) that are between > </ including the carriage return line feed, if any, between all the tags from the string:

<A t="1"> ad </A> <B t="2"> ba </B>
<C t="3"> rr </C>

It should become this:
<A t="1">ad</A><B t="2">ba</B><C t="3">rr</C>

Also, can someone kindly explain to me what these reg Exp are doing?

s=s.replace(/(<)(input|img|br|hr)([^>]*)/ig,"$1$2 $3 /")

s=s.replace(/=([^"]{1}[^ ">]*)/ig,"=\"$1\"")

s=s.replace(/ (selected|checked|multiple)/ig,function(s,m){x=m.toLowerCase();return " "+x+"=\""+x+"\""})

s=s.replace(/<(\/?[A-Z]+)/ig,function(s){return ""+s.toLowerCase()})

Thanks a lot

Pall
 
Old October 19th, 2007, 03:28 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii pallone!!

Hope this will help you

<script>

function CallMe()
{
 instr=document.getElementById("input").value



regExp1=/>\s*/g
outstr=instr.replace(regExp1,">")

regExp2=/\s*</g
outstr=outstr.replace(regExp2,"<")

document.getElementById("output").value=outstr
}
</script>
 <input type=button onclick="CallMe()" value="TestRegExp">


<br>
Input String
<textarea id="input" rows=4 cols=60></textarea>
<br>
output String
<textarea id="output" rows=4 cols=60></textarea>


Cheers :)

vinod
 
Old October 19th, 2007, 03:59 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi vinod,

Your example is working. That is brillant. Thanks for you help.

Would it be possible to achieve the same result using only one regular expression?

I have seen some examples online where developers use a $1 to replace. Here are some of the examples I found online but I do not understand it. The first example uses 3 $1$2 $3 to replace.

Could you please elaborate a bit on these?

s=s.replace(/(<)(input|img|br|hr)([^>]*)/ig,"$1$2 $3 /")


s=s.replace(/=([^"]{1}[^ ">]*)/ig,"=\"$1\"")


Cheers

Pallone





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression Help Please rstelma ASP.NET 1.0 and 1.1 Professional 0 January 2nd, 2008 07:01 PM
REGULAR EXPRESSION pallone Javascript How-To 0 September 5th, 2006 06:52 AM
regular expression if-then smay Javascript 4 July 5th, 2005 12:10 PM
Regular Expression Help Greg Griffiths Javascript How-To 4 November 12th, 2004 05:33 AM
Regular Expression Help Hadware .NET Web Services 3 November 4th, 2003 10:51 AM





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