/    Sign up×
Community /Pin to ProfileBookmark

Parse Question

In the following script, can someone explain where the extra ‘,,,,’ are comming from, and better yet, how to get rid of them.

For example: 123+456 gives an alert message of ,123,,,,+,,456,,
What I would like to see is: 123,+,456

[code=php]
<html>
<head>
<title>Parse</title>
<script type=”text/javascript”>
function strToTokens(str) {
str = str.replace(/(s+)/g,’ ‘);
var tokens = new Array();
var tokens_index = 0;
tokens = str.split(/(w+)|(W+)/i);
alert(‘StrToTokens:’+tokens);
}
</script>
</head>
<body>
Parse Test<br>
<input type=”text” id=”StrInfo” value=””>
<button onClick=”strToTokens(document.getElementById(‘StrInfo’).value)”>Action</button>
<input name=”tst” type=”radio” value=”123+456″ onClick=”document.getElementById(‘StrInfo’).value=this.value”>
<input name=”tst” type=”radio” value=”a=12+(-34)+56″ onClick=”document.getElementById(‘StrInfo’).value=this.value”>
<input name=”tst” type=”radio” value=”c2=a^2+b^2″ onClick=”document.getElementById(‘StrInfo’).value=this.value”>
</body>
</html>

[/code]

The extra radiobuttons are just for testing setup purposes.

Ideally I would like to split a string into Numbers, Variables (letters & numbers) and other characters like *,/,!, etc. to use in an Infix to Postfix (RPN) conversion script.

Any help is appreciated.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 01.2007 — str.split(/(w+)|(W+)/i); [/QUOTE]
If you are splitting the input string on either lowercase w's or uppercase W's,

you don't need the i flag. Those are the only two cases w has.

But I don't see where all those ws are coming from.

And why did you replace sequences of the letter s with a space?
str = str.replace(/(s+)/g,' '); [/QUOTE]
And what is poor tokens_index doing?
Copy linkTweet thisAlerts:
@JMRKERauthorMar 01.2007 — Thanks for the reply "mrhoo"

If you are splitting the input string on either lowercase w's or uppercase W's,

you don't need the i flag. Those are the only two cases w has.[/QUOTE]


I'll get rid if the i flag. It was left over from a previous attempt when I was using something like /(s+)|(d+)/i and /ig test expressions...

But I don't see where all those ws are coming from.[/QUOTE]

When I used only the w or only the W, I got only numbers and variables with letters. It left out the other characters in the string.

And why did you replace sequences of the letter s with a space?
[/QUOTE]


This was to remove any extra spaces between the "tokens" so they would not show up as blank tokens. In an earlier attempt I had used s as a split value and was getting incorrect results with extra spaces in the entries.

And what is poor tokens_index doing?[/QUOTE]
You're right, it is not needed. Again, it was left over from a previous attempt before I tried getting help from this forum.

Thanks for your response ... any other ideas? I'm definitely not an expert on regular expression (regex) formations. It just looked like a good idea at the time.
Copy linkTweet thisAlerts:
@JMRKERauthorMar 01.2007 — Bump.

Any ideas from the mid-day crowd?

All my late-night lookers seem to have gone to sleep!
Copy linkTweet thisAlerts:
@mrhooMar 01.2007 — Try matching:

[CODE]function strToTokens(str){
var tokens= str.match(/(w+)|(W+)/g)
alert('StrToTokens:'+tokens.join(','));
}[/CODE]

strToTokens('124+45-23'); // returns>> '124,+, 45,-, 23'
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...