4

below is something I am trying to do with JavaScript.

If I have string like

str = "how are you? hope you are doing good" ;

now I want to split it with ? (or . or !) but I dont want to lose the "?". Instead I want to break the string just after the question mark such a way that question mark is with the first segment that we have. also after the ? or / or ! there needs to be a \s (space) in order to break it into segments

after splitting str what should I get is

["how are you?","hope you are doing good"]

I am not sure if it can be done with Javascript split() function ,please help.

1
  • We could have done str.split(/(?<=\?)\s+/); but unfortunately javascript doesn't support lookbehind. Commented May 12, 2010 at 7:03

6 Answers 6

12
str.match(/\S[^?]*(?:\?+|$)/g)

["how are you?", "hope you are doing good"]
Sign up to request clarification or add additional context in comments.

5 Comments

Just curious, is there a reason you chose to use \S[^?]* over [^?]+?
just to strip leading whitespaces, @Kevin.
Ahh okay, makes sense. Initially read it wrong. Maybe it's time for bed. Anyway, this is definitely the way to do it. Green check mark here!
Nice and simple! But fails with two "?" in a row, e.g. "how are you?? hope you are doing good" => ["how are you?", "? hope you are doing good"] If you drop the whitespace thing (\S), that problem goes away (and you can just clean it after).
@T.J. Crowder, updated \? to \?+ to support that ?? pattern, If I drop \S it would return 2nd one as " hope you are doing good"
4

The easiest, most straight-forward way I can see is to just lose the "?" and add it back:

var parts, n;
parts = str.split("?");
for (n = parts.length - 2; n >= 0; --n) { // Skip the last one
    parts[n] += "?";
}
if (str.charAt(str.length-1) == "?") {    // If it ended with "?", add it back
    parts[parts.length-1] += "?";
}

Alternately, though, there's a way via RegExp. Edit Removed mine, S.Mark's regex is so much better.

Comments

2

...

var str = "how are you? hope you are doing good";
var data = str.split('?');
data[0] += '?';

alert(data[0]); // result: how are you?
alert(data[1]); // result: hope you are doing good

Comments

2

One option is to use a regex, as in:

str = "how are you? hope you are doing good";
var tokens = str.split(/(\?)/)

but this wil result in the question marks in their own token: how are you,?, hope you are,?, doing good

A better option is:

var tokens = str.match(/[^?]+\?*/g)

This will also keep multiple question marks: "hello??? how are you?"

Comments

1

If JS supported regexp look-behinds all you'd need is split by /(?<=\?)/, but unfortunately it doesn't. However you can very easily get the same behavior with the match() function:

var str = "how are you? hope you are doing good";
var data = str.match(/(?!$)[^?]*\??/g);

Will give you ["how are you?", " hope you are doing good"]

1 Comment

I tried to change this regex to in order to work with . & ! as well , for example instead of only ? if the condition is like "\?\s" or "\!\s" or "\.\s" . in this case what would be the expression ? so that for sentence like "This is a good example to show? this example to show is good.ok do it" then what we get is ["This is a good example to show?","this example to show is good.ok do it"] as there is no space after the . (fullstop)
1

another idea:

var str = "how are you? hope you are doing good",
    strsplit = str.split('?').join('?#').split('#');
alert(strsplit) //=> how are you?, hope you are doing good

or (based on answer & comment from kobi)

var str = "how are you? All right?? Hope so???";
alert(str.replace(/([\?(1,}][^\?$])/g,'$1#!#').split(' #!#'));
  //=>how are you?,All right??,Hope so???

1 Comment

might as well replace(/\?/g, '?###'), but you're taking a risk if the string already has # signs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.