0

I have a long string that a user entered. Using javascript I want to get the text of the first sentence IF it ends in a question mark. Is there a simple way I can do this?

For example:

var myText1 = "What is your name? My name is Michelle."

I need to return: "What is your name"

var myText2 = "this is a test. this is a test."

I need to return: "n/a"

askedAug 28, 2011 at 17:39
michelle's user avatar
6
  • Depends how reliable you want it to be. What constitutes a sentence for you?CommentedAug 28, 2011 at 17:41
  • Must be the first block of text that ends in a ". " or "? " I don't need to do a very strong test so for example even the string "abc 1. and xx." would be a sentence "abc 1"CommentedAug 28, 2011 at 17:43
  • Then how would you handle"Btw. what's your name?"? (abbreviations in general)CommentedAug 28, 2011 at 17:44
  • I would handle it as "btw." which returns "n/a"CommentedAug 28, 2011 at 17:46
  • 1
    Or "How are you today Mr. Willoughsby?"CommentedAug 28, 2011 at 17:46

3 Answers3

2

Regex to the rescue:

var res = str.match(/^([^.]+)\?/);var output = (res == null) ? 'n/a' : res[1];
answeredAug 28, 2011 at 17:57
Aillyn's user avatar
Sign up to request clarification or add additional context in comments.

Comments

0

I'm confused as to how practical this would be, but this should work.

var text = "this is a question? this is some text.";var split1 = text.split(/[.?]+/); // splits the text at "." and "?"var split2 = text.split(/[?]+/); // splits the text at "?"// if the first element in both arrays is the same, the first sentence is a question.var result = (split1[0] == split2[0]) ? split1[0] : "n/a";
answeredAug 28, 2011 at 17:58
gargantuan's user avatar

2 Comments

Wouldn't this fail if text doesn't have either. and? (eg: if text was"hello", result would be"hello")
Absolutely, although the OPs definition of a sentence was "Must be the first block of text that ends in a . or ?". I personally think the whole premise is flawed and that a much more sophisticated approach is needed. But this works for what was asked.
0

According to US English grammar rules, sentences end in.,? or!, but if the sentence ends in a quotation then a" will follow(He said to me, "How are you doing?"). Does this count as a question? The sentence itself is a statement quoting a question, so I'll assume the answer is no. This makes it easier since we only have to account for? not followed by a".

Given the above, here's my solution:

function startIsQuestion( str ) {  var m = str.match(/^[^.!]+[?][^"]/);  if (!m ||       m[0].indexOf('.') >= 0 ||      m[0].indexOf('!') >= 0 ||      m[0].indexOf('?"') >= 0) return "n/a";  return m[0];}

I don't think it's completely robust, but I'm not sure of your complete requirements and it should give you a good start.

See demo

answeredAug 28, 2011 at 18:11
mVChr's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.