471,338 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 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 1009
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Patchwork | last post: by
1 post views Thread by Proteus | last post: by
7 posts views Thread by abcd | last post: by
14 posts views Thread by Giancarlo Berenz | last post: by
30 posts views Thread by galiorenye | last post: by
10 posts views Thread by Phillip Taylor | last post: by
17 posts views Thread by Chris M. Thomasson | last post: by
reply views Thread by rosydwin | last post: by

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.