Simple RegExpresssion question

W

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
 
M

McKirahan

wl said:
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.split("=");
what += "<br>" + item[0] + " = " + item[1];
}
document.write(what);
</script>
</head>
<body>
</body>
</html>

Why is "RegExpression" in your Subject?
 
W

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.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
 
R

RobG

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:

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.
 
W

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 said:
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:

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.
 
M

McKirahan

wl said:
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.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.
 
R

RobG

wl said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top