Regex: String Match Including Punctuation
From another question, I have this expression to match words in a sentence: It works perfectly. However, now I am looking for a way to match exclamation marks, question marks, and
Solution 1:
Try Below Code
var sentence = "Exclamation! Question? Full stop. Ellipsis...";
console.log(sentence.toLowerCase().match(/[?!.]|\w+/g));
In case You want only one dot, you could use something like ---
var sentence = "Exclamation!!! Question??? Full stop. Ellipsis...";
var arr = sentence.toLowerCase().match(/[?]+|[!]+|[.]+|\w+/g);
arr = arr.map(function(item){
return item.replace(/(.)\1+/g, "$1");
})
console.log(arr);
Solution 2:
How about using a word boundary to only return one dot from the ellipsis?
var sentence = "Exclamation! Question? Full stop. Ellipsis...";
console.log(sentence.toLowerCase().match(/[a-z]+(?:'[a-z]+)*|\b[!?.]/g));
Or a negative lookahead:
var sentence = "Exclamation! Question? Full stop. Ellipsis...";
console.log(sentence.toLowerCase().match(/[a-z]+(?:'[a-z]+)*|[!?.](?![!?.])/g));
After your commented scenario extension, a negative lookbehind seems to be effective.
var sentence = "You're \"Pregnant\"??? How'd This Happen?! The vasectomy YOUR 1 job. Let's \"talk this out\"...";
console.log(sentence.toLowerCase().match(/[a-z\d]+(?:'[a-z\d]+)*|(?<![!?.])[!?.]/g));
Post a Comment for "Regex: String Match Including Punctuation"