The more you look at these things the more they can be simplified
Code:
(?:^|[.;!?])\s*([^.!?]*happy[^.!?]*)(?:[.?!]|$)
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Your conditions are self contradictory, that's why it's hard to do it all in one RegExp.
"If a capital letter is not found (going backwards from the word to be found), then look for a [.?!]. If a [.?!] is not found look for the beginning of the text."
So the search for punctuation can only take place *IF* the search for a capital letter fails.
And the search for beginning of text can only take place *IF* both the other searches failed.
On top of that, you *want* to capture the capital letter if it is there but you do *NOT* want the punctuation but you *DO* want the start of text ... UGH!
Maybe a true RegExp guru can come up with one that works. I can't. Or at least I can't without spending who knows how much time on it.
Why is it important to be a single RegExp?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
I just need to get rid of that leading full`stop and space before the capital (if there is one)
My latest version doesn't capture the full-stop and space. It does if you look at the entire capture, but the text you need is in capture group 1.
Added: To retrieve the captured group 1:
Code:
var match = myRegexp.exec(myString);
alert(match[1]); // abc
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 02-01-2013 at 01:10 AM..
"If a capital letter is not found (going backwards from the word to be found), then look for a [.?!]. If a [.?!] is not found look for the beginning of the text."
that was never a condition. Again:
- Start capturing from the closest word before the variable word that starts with a capital/uppercase.
- If there is no word that starts with a capital before the variable word, start capturing from the start of the string.
- Equally, if the part of the string after the variable word contains a full stop, exclamation mark or question mark, finish capturing at the punctuation.
- If not, capture until the end of the string.
I don't want the punctuation at the start. Only at the end, if it's a [.?!]
The Latin adverb sic ("thus"; in full: sic erat scriptum, "thus was it written") added immediately after a quoted word or phrase (or a longer piece of text), indicates that the quotation has been transcribed exactly as found in the original source, complete with any erroneous spelling or other nonstandard presentation. The usual purpose is to inform the reader that any errors or apparent errors in the transcribed material do not arise from transcription errors, and the errors have been repeated intentionally, i.e., that they are reproduced exactly as set down by the original writer or printer. It may also be used as a form of ridicule or as a humorous comment, drawing attention to the original writer's spelling mistakes or emphasizing his or her erroneous logic.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
I don't want the punctuation at the start. Only at the end,
I don't recall you mentioning that you want to capture the punctuation at the end so mine doesn't capture it. It should be easy to modify to include it.
Added: I've edited a recent post of mine to show how you can retrieve capture group 1.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 02-01-2013 at 01:11 AM..
var myvar = "happy";
var myreg = new RegExp("(?:^|[.;!?])\s*([^.!?]*" + myvar + "[^.!?]*)(?:[.?!]|$)");
var matches = myreg.exec("Hey! Some great and happy sentence! Doh!");
console.log(matches[1]);
(untested)
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 02-01-2013 at 01:22 AM..
My last one! I think I'll ignore spaces at the beginning or end of the sentence and just trim afterwards:
Code:
var myvar = "happy";
var myreg = new RegExp("(?:^|[.;!?])([^.!?]*" + myvar + "[^.!?]*)(?:[.?!]|$)");
var matches = myreg.exec("Hey! Some great and happy sentence ! Doh!");
var sentence = (matches[1]).trim();
console.log(sentence); // Some great and happy sentence
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 02-01-2013 at 01:31 AM..
I think he was using (sic) [sic] in reference to "capital letters" versus "upper case letters."
But since he used it wrongly (parentheses instead of square brackets), he doesn't score any points. <grin/>
I shouldn't have to explain this.. but.. my sentence didn't begin within a capital letter, and I was stating that I always begin sentences with a capital letter ..hence.. oh, never mind
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
I'm not sure that you can quote yourself in the sentence where you are quoting yourself, saying "thus was it written" as you are in the act of writing it.
Sounds like a little too much recursion to me (my first javascript joke and I am very proud of it).
I'm not sure that you can quote yourself in the sentence where you are quoting yourself, saying "thus was it written" as you are in the act of writing it.
Sounds like a little too much recursion to me (my first javascript joke and I am very proud of it).
Quote:
Wiki: It may also be used as a form of ridicule or as a humorous comment, drawing attention to the original writer's spelling mistakes or emphasizing his or her erroneous logic
.
..but this is also recursive as I am quoting a quote that I already quoted.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS