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 01-31-2013, 10:01 PM   PM User | #16
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 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 notice you using [^.] instead of [^\.\?\!] so I changed that:
[code]
/(?:^|\.|\?|\!)?[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)/

Different results, but still not right.

Also it would seem that (?:^|\.|\?|\!)? is doing nothing at all.

I removed it completely, to get this:
/[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)/
and got the same (wrong) results.
__________________
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 offline   Reply With Quote
Old 01-31-2013, 10:27 PM   PM User | #17
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
Code:
I will say that your (?:^|\.|\?|\!) seemed to have mostly worked. Surprised me.
Yes, I forgot a | in this expression. ^ means the beginning of the line when it is outside of a character class.

So this is a non-capturing group that will be either the beginning of a line (or the string), a full-stop, etc..

But, no, I'm not testing it as thoroughly as you are.
__________________
"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 01-31-2013, 10:39 PM   PM User | #18
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
Well this works with the expressions xelawho proposed, but only if it starts with a Capital letter (the original request):

Code:
(?:^|\.|\;|\!|\?)\s?([A-Z][^.]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
but again I'm not testing it beyond the sample phrases, so I won't be offended if it is dropped

Revision:

Code:
(?:^|\.|\;|\!|\?)\s?([A-Z][^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
Attached Thumbnails
Click image for larger version

Name:	regex2.png
Views:	3
Size:	17.0 KB
ID:	11908  
__________________
"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; 01-31-2013 at 10:49 PM..
AndrewGSW is offline   Reply With Quote
Old 01-31-2013, 10:42 PM   PM User | #19
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 notice you using [^.] instead of [^\.\?\!] so I changed that
@Old Pedant - yes, the ! and ? weren't mentioned until recently
__________________
"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 01-31-2013, 10:48 PM   PM User | #20
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
These two pages could maybe inspire a solution : Highlight a word and Select a Sentence
007julien is offline   Reply With Quote
Old 01-31-2013, 10:53 PM   PM User | #21
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 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
Quote:
Originally Posted by AndrewGSW View Post
Yes, I forgot a | in this expression. ^ means the beginning of the line when it is outside of a character class.
I knew that. DOH. I was just completely misreading the intent of that sub-expression. Double DOH on me.
__________________
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 offline   Reply With Quote
Old 01-31-2013, 11:35 PM   PM User | #22
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
looks to me like
Code:
(?:^|\.|\?|\!)?[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
and
Code:
[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
are the only one that fulfill the 4 requirements

(the rule is not that it must begin with a capital letter, but that if there is no capital letter preceding the keyword, it captures the string from the start)

thanks everybody!
xelawho is offline   Reply With Quote
Old 01-31-2013, 11:43 PM   PM User | #23
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 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
Quote:
Originally Posted by xelawho View Post
(the rule is not that it must begin with a capital letter, but that if there is no capital letter preceding the keyword, it captures the string from the start)
And what about a period that is *NOT* followed by a capital letter?

aardvarks whistle. dogs jump when they are happy

Given your rule as stated here, that would return the *entire* string.

Is that truly what you want?
__________________
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 offline   Reply With Quote
Old 01-31-2013, 11:45 PM   PM User | #24
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
I suppose my version without requiring a capital would be

Code:
(?:^|\.|\;|\!|\?)\s?([\w][^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
but all my sentences begin with capital letters (sic.) and end with a smile

Just for completeness (for anyone reading) to embed a string in a regex requires use of the RegExp constructor:

Code:
var re = new RegExp("^\\/" + resource + "\\/\\d+$");
__________________
"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; 01-31-2013 at 11:49 PM..
AndrewGSW is offline   Reply With Quote
Old 01-31-2013, 11:53 PM   PM User | #25
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
looks to me like
Code:
(?:^|\.|\?|\!)?[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
and
Code:
[\w]([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
are the only one that fulfill the 4 requirements

(the rule is not that it must begin with a capital letter, but that if there is no capital letter preceding the keyword, it captures the string from the start)

thanks everybody!
Don't these versions drop the first letter?

Added: I think I would account for zero or more spaces at the beginning as well:

Code:
(?:^|\.|\;|\!|\?)\s*([\w][^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
and also if happy is the very first word:
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

Last edited by AndrewGSW; 01-31-2013 at 11:58 PM..
AndrewGSW is offline   Reply With Quote
Old 02-01-2013, 12:14 AM   PM User | #26
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
no - they both capture the full stop at the end of the preceding sentence.
xelawho is offline   Reply With Quote
Old 02-01-2013, 12:24 AM   PM User | #27
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by Old Pedant View Post
And what about a period that is *NOT* followed by a capital letter?

aardvarks whistle. dogs jump when they are happy

Given your rule as stated here, that would return the *entire* string.

Is that truly what you want?
Yes. This is to do spellchecks on content editable divs on a website for professional writers. If they're starting their sentences with lower case letters they have bigger problems than a few extra characters in their output.

maybe a little detail will help. The spellchecker scans the text nodes, so if the innerHTML is
Code:
I had a <b>great</b> day
it will scan 3 chunks:
1) I had a
2) great
3) day

Which is kind of meaningless there, but there could be an entire paragraph without formatting and so you just want to show a snippet. A sentence would be ideal, but by the time the spellchecker gets to the text it's all broken up into text nodes. So instead of piecing it back together, there are the four rules (rule 3 revised halfway through this thread):

- 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.

Make sense?
xelawho is offline   Reply With Quote
Old 02-01-2013, 12:25 AM   PM User | #28
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 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
Well, I gave you my answer, using 4 regexps. Which I see no reason can't be combined into one. And then one more regexp eliminates not only any preceding [.?!] but also trims the string on the left side of any spaces.

Unless you are a fanatic about doing it all with a single regexp, I don't see what is wrong with that. What difference does it make if it's 1 regexp or 2 or 5?
__________________
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 offline   Reply With Quote
Old 02-01-2013, 12:27 AM   PM User | #29
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
but you're right Andrew - neither of the ones that I thought were winners in #22 capture anything if the word comes at the start of the string
xelawho is offline   Reply With Quote
Old 02-01-2013, 12:37 AM   PM User | #30
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
but you're right Andrew - neither of the ones that I thought were winners in #22 capture anything if the word comes at the start of the string
I haven't tested my most recent solution in great depth but I'll repeat it here:
Quote:
and also if happy is the very first word:
Code:
(?:^|\.|\;|\!|\?)\s*([^\.\!\?]*happy[^\.\!\?]*)(?:\.|\?|\!|$)
All those escapes \, though, are not necessary in a character class - Added: I'm blaming OP for this (I wouldn't have included them )

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

Last edited by AndrewGSW; 02-01-2013 at 01:34 AM..
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 10:53 AM.


Advertisement
Log in to turn off these ads.