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"
- Depends how reliable you want it to be. What constitutes a sentence for you?Felix Kling– Felix Kling2011-08-28 17:41:51 +00:00CommentedAug 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"michelle– michelle2011-08-28 17:43:48 +00:00CommentedAug 28, 2011 at 17:43
- Then how would you handle"Btw. what's your name?"? (abbreviations in general)Felix Kling– Felix Kling2011-08-28 17:44:24 +00:00CommentedAug 28, 2011 at 17:44
- I would handle it as "btw." which returns "n/a"michelle– michelle2011-08-28 17:46:10 +00:00CommentedAug 28, 2011 at 17:46
- 1Or "How are you today Mr. Willoughsby?"numbers1311407– numbers13114072011-08-28 17:46:46 +00:00CommentedAug 28, 2011 at 17:46
3 Answers3
Regex to the rescue:
var res = str.match(/^([^.]+)\?/);var output = (res == null) ? 'n/a' : res[1];Comments
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";2 Comments
. and? (eg: if text was"hello", result would be"hello")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.
Comments
Explore related questions
See similar questions with these tags.