/    Sign up×
Community /Pin to ProfileBookmark

How to split string by seperator not in quotation?

Thanks ahead.

I have a regex question, I have string like

userage “please input your age.” %1 %2

I want to split it, so I get four pieces like

userage
“please input your age.”
%1
%2

that means seperate it by space, but not include those spaces in quotation, any suggestion?

Thanks!

to post a comment
JavaScript

18 Comments(s)

Copy linkTweet thisAlerts:
@JonaAug 29.2004 — [font=trebuchet ms]Split it by the quotes first (".+"), then the remaining string split by whitespace (s+).[/font]
Copy linkTweet thisAlerts:
@CharlesAug 29.2004 — Better yet, don't split it. Just use the regular expression to grab what you want …

[font=monospace]<script type="text/javascript">

<!--

String.prototype.grab = function () {return /userage "please input your age." (S+) (S+)/.test(this) ? [RegExp.$1, RegExp.$2] : null}

alert ('userage "please input your age." %1 %2'.grab())

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@JonaAug 29.2004 — [font=trebuchet ms]Charles, the only concern I'd have then is that perhaps the string is not always exactly the same in all parts.[/font]
Copy linkTweet thisAlerts:
@wyx2000authorAug 29.2004 — Thanks!

Sounds a good solution, I was always looking for one statement to do that.

Just tried it, split userage "please input your age." %1 %2 with ".+", I only get two pieces:

userage

%1 %2

I think what I need is three pieces :

userage

"please input your age."

%1 %2

this way, I know which pieces include quotation and I can continue split others with spaces.

Any idea?
Copy linkTweet thisAlerts:
@JonaAug 29.2004 — [i]Originally posted by wyx2000 [/i]

[B]Thanks!



Sounds a good solution, I was always looking for one statement to do that.



Just tried it, split userage "please input your age." %1 %2 with ".+", I only get two pieces:



userage

%1 %2



I think what I need is three pieces :

userage

"please input your age."

%1 %2



this way, I know which pieces include quotation and I can continue split others with spaces.



Any idea? [/B]
[/QUOTE]


[font=trebuchet ms]Store the variable with parentheses. [i]"(.+)"[/i]

Charles' method may be more useful for you if you are looking for a relatively similar string each time, though.[/font]
Copy linkTweet thisAlerts:
@wyx2000authorAug 29.2004 — as you said they are not in fixed format. so charles's solution doesn't work for me.

just tried

var re = new RegExp(/"(.+)"/, "g");

var params='userage "please input your age." %1 %2'.split(re);

for(var j = 0; j <params.length; j++){

alert(params[j]);

}

it still only returns two pieces/
Copy linkTweet thisAlerts:
@JonaAug 29.2004 — <i>
</i>&lt;script type="text/javascript"&gt;
var re = /"(.+)"/g;
var params='userage "please input your age." %1 %2'.split(re);

for(var j = 0; j &lt;params.length; j++){
alert(params[j]);
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@wyx2000authorAug 29.2004 — hmm, your code doesn't look different from mine. And I just run it, got same results, only two pieces returned
Copy linkTweet thisAlerts:
@CharlesAug 29.2004 — I think we need a lot more information about just what this string is going to look like and just what we're trying to extract.
Copy linkTweet thisAlerts:
@JonaAug 29.2004 — [font=trebuchet ms]Hm, [url=http://209.169.115.234/jona/research/javascript/wyx2000/regex.html]it works for me[/url].[/font]
Copy linkTweet thisAlerts:
@JonaAug 29.2004 — [i]Originally posted by Charles [/i]

[B]I think we need a lot more information about just what this string is going to look like and just what we're trying to extract. [/B][/QUOTE]


[font=trebuchet ms]I second that; we have no clue as to what the possible strings would look like, other than what you showed us. Will they all follow the same format, exactly?[/font]
Copy linkTweet thisAlerts:
@wyx2000authorAug 29.2004 — [i]Originally posted by Jona [/i]

[B][font=trebuchet ms]Hm, [url=http://209.169.115.234/jona/research/javascript/wyx2000/regex.html]it works for me[/url].[/font] [/B][/QUOTE]


strange, I still only get two alerts from the your url

userage and %1, %2

those commands I use to tell one of my js function how to check user input, they will be lines like

hide %1

hide

admin %n{Admin level for this user|3|^[0-9]+$|3|7} %1

so first command take one parameter

second doesn;t need parameter

the third take two parameters, note here that they are a little different from what I asked in my question, take the third as an example, I use %1 to start a parameter, so there are two

%n{Admin level for this user|3|^[0-9]+$|3|7}

%1

%n = integer

%1 = predefined first object

there are other parameters like %s and more... so it is better to split the string by space, but the spaces in bracket, that means spaces in "Admin level for this user|3|^[0-9]+$|3|7" servers other purpose, what I tried to do is slit the string by space, but not include space in bracket.

Is it more clear to you?
Copy linkTweet thisAlerts:
@JonaAug 29.2004 — [font=trebuchet ms][url=http://209.169.115.234/jona/research/javascript/wyx2000/regex.html]Is this better[/url]? The reason it didn't work was because you're using Internet Explorer; it worked in Mozilla Firefox. ? [/font]
Copy linkTweet thisAlerts:
@wyx2000authorAug 29.2004 — [i]Originally posted by Jona [/i]

[B][font=trebuchet ms][url=http://209.169.115.234/jona/research/javascript/wyx2000/regex.html]Is this better[/url]? The reason it didn't work was because you're using Internet Explorer; it worked in Mozilla Firefox. ? [/font] [/B][/QUOTE]


It works, but not quite, I mean it seems the way it tells if it is a quoted string is not so ideal.

It is my fault, I should just ask this at the beginning. I have to do it now, since I still have no clue to get what I want, it is how to split this string

%n{Admin level for this user|3|^[0-9]+$|3|7} %1 %2

to pieces like

%n{Admin level for this user|3|^[0-9]+$|3|7}

%1

%2

really appreciate you guys' help.
Copy linkTweet thisAlerts:
@JonaAug 29.2004 — [font=trebuchet ms]To make it easier on us, use a multicharacter delimiter, instead of a space. E.g.: %n{Admin level for this user|3|^[0-9]+$|3|7}_***_%1_***_%2

That way, you can just split it by _***_, which is likely not to occur in your string.[/font]
Copy linkTweet thisAlerts:
@wyx2000authorAug 29.2004 — [i]Originally posted by Jona [/i]

[B][font=trebuchet ms]To make it easier on us, use a multicharacter delimiter, instead of a space. E.g.: %n{Admin level for this user|3|^[0-9]+$|3|7}_***_%1_***_%2



That way, you can just split it by _***_, which is likely not to occur in your string.[/font] [/B]
[/QUOTE]


Nice solution,? , you are right, maybe I should just make it work first.
Copy linkTweet thisAlerts:
@JonaAug 29.2004 — [font=trebuchet ms]There's always an alternative when it comes to JavaScript. Well, almost always. ? [/font]
Copy linkTweet thisAlerts:
@wyx2000authorAug 29.2004 — Maybe I always try to find a normal solution(if not best solution), just don't feel comfortable about some quick solution.
×

Success!

Help @wyx2000 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 5.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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