Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 12:43 AM   PM User | #31
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 02-01-2013, 12:44 AM   PM User | #32
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
I just can't believe it's so complicated it needs 4 regexes...

isn't it possible just to make this:
Code:
(?:^|\.|\;|\!|\?)\s*([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
start capturing at the first capital (and what's with the sic on capital?) instead of the full stop of the previous sentence?
xelawho is offline   Reply With Quote
Old 02-01-2013, 12:53 AM   PM User | #33
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by AndrewGSW View Post
The more you look at these things the more they can be simplified

Code:
(?:^|[.;!?])\s*([^.!?]*happy[^.!?]*)(?:[.?!]|$)
so close...

I just need to get rid of that leading full`stop and space before the capital (if there is one)
xelawho is offline   Reply With Quote
Old 02-01-2013, 12:54 AM   PM User | #34
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 02-01-2013, 01:00 AM   PM User | #35
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by xelawho View Post
so close...

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..
AndrewGSW is offline   Reply With Quote
Old 02-01-2013, 01:02 AM   PM User | #36
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by Old Pedant View Post
"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 [.?!]
xelawho is offline   Reply With Quote
Old 02-01-2013, 01:03 AM   PM User | #37
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Wiki: Sic
Quote:
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
AndrewGSW is offline   Reply With Quote
Old 02-01-2013, 01:05 AM   PM User | #38
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
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..
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
xelawho (02-01-2013)
Old 02-01-2013, 01:14 AM   PM User | #39
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
I know what sic means. I'm wondering what's wrong with capital letters:
http://www.britishcouncil.org/learne...al-letters.htm

[EDIT:] Sweet. Thanks for the example. I think I mentioned I'm a complete nong at regex
xelawho is offline   Reply With Quote
Old 02-01-2013, 01:16 AM   PM User | #40
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Edited, sorry! Had a missing bracket.
Code:
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..
AndrewGSW is offline   Reply With Quote
Old 02-01-2013, 01:29 AM   PM User | #41
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
trim() isn't available in some old browsers, so:
Code:
if(!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g,'');
  };
}
My work here is done!
__________________
"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..
AndrewGSW is offline   Reply With Quote
Old 02-01-2013, 02:25 AM   PM User | #42
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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/>
__________________
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.
Old Pedant is online now   Reply With Quote
Old 02-01-2013, 02:29 AM   PM User | #43
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by Old Pedant View Post
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
AndrewGSW is offline   Reply With Quote
Old 02-01-2013, 02:34 AM   PM User | #44
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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).
xelawho is offline   Reply With Quote
Old 02-01-2013, 02:44 AM   PM User | #45
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by xelawho View Post
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
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:24 AM.


Advertisement
Log in to turn off these ads.