473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Simple RegExpresssion question

wl
Hi,

I'm relatively new to Javascript and wanted to use the split function to
split a string into an array.
I need to split on every single occurence of = and &.

For example:
name=value&name2=another value with &&s and ==s

should be split in:
name
value
name2
another value with &&s and ==s

Thanks in advance,

Wim
Jul 23 '05 #1
6 1061
"wl" <so***@nospam.please> wrote in message
news:42***********************@news.skynet.be...
Hi,

I'm relatively new to Javascript and wanted to use the split function to
split a string into an array.
I need to split on every single occurence of = and &.

For example:
name=value&name2=another value with &&s and ==s

should be split in:
name
value
name2
another value with &&s and ==s

Thanks in advance,

Wim

Will this help? Watch for word-wrap.

<html>
<head>
<title>splits.htm</title>
<script type="text/javascript">
var data = "name1=value1&name2=value2&name3=value3";
var pair = data.split("&");
var what = data + "<br>"
for (var i=0; i<pair.length; i++) {
var item = pair[i].split("=");
what += "<br>" + item[0] + " = " + item[1];
}
document.write(what);
</script>
</head>
<body>
</body>
</html>

Why is "RegExpression" in your Subject?
Jul 23 '05 #2
wl
> Will this help? Watch for word-wrap.

<html>
<head>
<title>splits.htm</title>
<script type="text/javascript">
var data = "name1=value1&name2=value2&name3=value3";
var pair = data.split("&");
var what = data + "<br>"
for (var i=0; i<pair.length; i++) {
var item = pair[i].split("=");
what += "<br>" + item[0] + " = " + item[1];
}
document.write(what);
</script>
</head>
<body>
</body>
</html>

Why is "RegExpression" in your Subject?


Hi,

I don't want the double &'s and ='s to cause a split to be done. I'm in the
assumption this can be done
by using Regular Expressions ("split on each occurence of '&' and '=' unless
these are resp. followed by another '&' and '=')

Thanks,

Wim
Jul 23 '05 #3
wl wrote:
[...]

Hi,

I don't want the double &'s and ='s to cause a split to be done. I'm in the
assumption this can be done
by using Regular Expressions ("split on each occurence of '&' and '=' unless
these are resp. followed by another '&' and '=')


From your original post:
For example:
name=value&name2=another value with &&s and ==s


I guess you mean:

name1=value1&name2=value2&=&name3=value3 etc.

Though I can't see how you get '&='. If the control is successful,
its name should be submitted even if the value is empty. If it's
unsuccessful, nothing will be submitted so you should get something
like:

name1=value1&name2=value2&name3=&name4=value4 etc.

Given your original '&=' the following should do the trick. If you
really meant the pattern above (i.e. change:

name1=value1&name2=&name3=value3

to get rid of empty name2 and have:

name1=value1&name3=value3

then you want to change the first replace pattern to /&(\w)*=&/g,'&':

<input type="text" size="60"
value="name1=value1&name2=value2&=&name3=value3&=& ="
onblur="
var x = this.value.replace(/&=/g,'');

// Alternative pattern replace
// var x = this.value.replace(/&(\w)*=&/g,'&');

x = x.replace(/=/g,'&').split('&');
alert(x.join('\n')); // x.join('<br>') if writing HTML
">

Any you're done.
--
Rob
Jul 23 '05 #4
wl
Hi Rob,

Thanks for your reply, but ... not really ...

What I'm looking for actually is a regular expression that detects
every DOUBLE occurence of & or DOUBLE occurence of =
And use this in a split ...

Thanks,

Wim

"RobG" <rg***@iinet.net.auau> wrote in message
news:xd*****************@news.optus.net.au...
wl wrote:
[...]

Hi,

I don't want the double &'s and ='s to cause a split to be done. I'm in
the assumption this can be done
by using Regular Expressions ("split on each occurence of '&' and '='
unless these are resp. followed by another '&' and '=')


From your original post:
For example:
name=value&name2=another value with &&s and ==s


I guess you mean:

name1=value1&name2=value2&=&name3=value3 etc.

Though I can't see how you get '&='. If the control is successful,
its name should be submitted even if the value is empty. If it's
unsuccessful, nothing will be submitted so you should get something
like:

name1=value1&name2=value2&name3=&name4=value4 etc.

Given your original '&=' the following should do the trick. If you
really meant the pattern above (i.e. change:

name1=value1&name2=&name3=value3

to get rid of empty name2 and have:

name1=value1&name3=value3

then you want to change the first replace pattern to /&(\w)*=&/g,'&':

<input type="text" size="60"
value="name1=value1&name2=value2&=&name3=value3&=& ="
onblur="
var x = this.value.replace(/&=/g,'');

// Alternative pattern replace
// var x = this.value.replace(/&(\w)*=&/g,'&');

x = x.replace(/=/g,'&').split('&');
alert(x.join('\n')); // x.join('<br>') if writing HTML
">

Any you're done.
--
Rob

Jul 23 '05 #5
"wl" <so***@nospam.please> wrote in message
news:42**********************@news.skynet.be...
Hi Rob,

Thanks for your reply, but ... not really ...

What I'm looking for actually is a regular expression that detects
every DOUBLE occurence of & or DOUBLE occurence of =
And use this in a split ...

Thanks,

Wim

How about this?

<html>
<head>
<title>splitz.htm</title>
<script type="text/javascript">
var data = "name=value&name2=another value with &&s and ==s";
data = data.replace(/\&\&/g,"^1")
data = data.replace(/\=\=/g,"^2")
var pair = data.split("&");
var what = data + "<br>"
for (var i=0; i<pair.length; i++) {
var item = pair[i].split("=");
what += "<br>" + item[0];
what += "<br>" + item[1];
}
what = what.replace(/\^1/g,"&&")
what = what.replace(/\^2/g,"==")
document.write(what);
</script>
</head>
<body>
</body>
</html>

This displays the results originally requested.

We were confused by your description:
"I need to split on every single occurence [sic] of = and &."
"every single" can be read as "every";
perhaps, "individual" instead of "single" would been better.
However, your example should have been sufficient.
Jul 23 '05 #6
wl wrote:
Hi Rob,

Thanks for your reply, but ... not really ...

What I'm looking for actually is a regular expression that detects
every DOUBLE occurence of & or DOUBLE occurence of =
And use this in a split ...


Please don't top-post.

So you want to match '&&' and '==' ? Surely you can work that out
from the examples given.

How about you provide an example input string, then what you want it
to look like after it's been processed. It's kinda off-putting to
have to guess what you want.
--
Rob
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
1
by: Proteus | last post by:
Any help appreciated on a small perl project I need to write for educator/teaching purposes. I have not programmed perl for some time, need to get up to speed, maybe some kind souls hrere will help...
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
1
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.