Lasse Reichstein Nielsen wrote:
"Random" <ra*******@gmai l.com> writes:
I just assumed \b\S* would provide the closest thing to the behavior he
expected, because the \b would handle some punctuation. But I agree
about \w.
It's also worth remembering that \b is defined in terms of "word
characters" (\b matches a zero-width position where there is a word
character on one side and not the other), and \w matches exactly a
word character, so
"\\b\\w*" + Char + "\\w*\b"
should match a word containing the Char (assuming that Char contains
a word character. If it isn't, then it might need to be escaped,
something like:
"\\b\\w*" + (/^\w$/.test(Char) ? Char : "\\"+Char) + "\\w*\b"
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Excellent points.
Curious, I checked it out. In my example of leftWordfromCha r():
Checking For Gave
test e test
test ing e test
test, ing e test
test!ing e test!ing
test! ing e test
test,ing e test,ing
test-ing e test-ing
-test- e test
_test_ e _test_
,test, e test
t(es)t-ing e t(es)t-ing
test ing \s test ing
test\ning \n test\ning (where \n = newline in all three)
test\ning \\n test\ning (where \n = newline in Checking and
Gave)
-- which made me realise that I didn't bother to check whether Char is
actually a single character. Guess I should have called it
leftWordfromStr ing.
Points being: some unpredictable behaviour may result, at the expense
of a broader definition of a 'word', something I probably should have
noted; escaping should be added if it is desired, something I
definitely should have pointed out.
Perhaps a better definition of a 'word' would be:
\w*(\w+[-']{1})*\w+
That would expand the definition of a 'word' to include contractions
and complex words (hyphenated), but would also match substrings
containing (but not terminated by) multiple non-sequential apostrophes.
In some cases, words may also begin or end with apostrophes ('Tis,
'til, an', et cetera) to indicate truncation-- this RE wouldn't match
the apostrophe. I don't think that last one can be accounted for.