469,904 Members | 2,074 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

Idea: Retrieve a Word from a string using one of the letters in that word

I have tried several time to do this but have been unsucessful.
I tried something like:

myFunction(charater)

str=frm.s1.value
sb1=str.substring(0,charater)
sb2=str.substring(charater,str.length)
//looping function to find nearest space on left side
for(parameters){
sbs1=str.indexOf(' ')
}
//found the space on the right no problem
sb2=str.indexOf(' ')

this is about as far as i got. Anyone that can help?

Best Regards,
Sandfordc
www.JavaScript-Central.tk

Jul 23 '05 #1
11 1764
Lee
Sandfordc said:

I have tried several time to do this but have been unsucessful.
I tried something like:

myFunction(charater)

str=frm.s1.value
sb1=str.substring(0,charater)
sb2=str.substring(charater,str.length)
//looping function to find nearest space on left side
for(parameters){
sbs1=str.indexOf(' ')
}
//found the space on the right no problem
sb2=str.indexOf(' ')

this is about as far as i got. Anyone that can help?


I can't tell what you're trying to do from those odd fragments of code.
If you can describe what you want, clearly and precisely, you'll
have a good start at coding it.

Jul 23 '05 #2
Sandfordc wrote:
I have tried several time to do this but have been unsucessful.
I tried something like:

myFunction(charater)

str=frm.s1.value
sb1=str.substring(0,charater)
sb2=str.substring(charater,str.length)
//looping function to find nearest space on left side
for(parameters){
sbs1=str.indexOf(' ')
}
//found the space on the right no problem
sb2=str.indexOf(' ')

this is about as far as i got. Anyone that can help?

Best Regards,
Sandfordc
www.JavaScript-Central.tk

If I get this right, you're trying to find the leftmost word containing
a provided character.

Regular Expressions can do this easily.

function leftWordfromChar( Line, Char ) {
if( ! Line || ! Char ) return null;

return Line.match(
RegExp( '\\b(\\S*' + Char + '\\S*)\\b', 'i' )
)[1];

}

alert( leftWordfromChar( 'Hey, look at my shoe!', 'e' ) );

Will alert: 'Hey'
This is case-insensitive. To make it case-sensitive, remove the
, 'i'
from
RegExp( '\\b(\\S*' + Char + '\\S*)\\b', 'i' )

You may consider making this an option of your function.

Jul 23 '05 #3
On Wed, 01 Jun 2005 21:01:58 -0700, Sandfordc wrote:
I have tried several time to do this but have been unsucessful.
I tried something like:

myFunction(charater)

str=frm.s1.value
sb1=str.substring(0,charater)
sb2=str.substring(charater,str.length)
//looping function to find nearest space on left side
for(parameters){
sbs1=str.indexOf(' ')
}
//found the space on the right no problem
sb2=str.indexOf(' ')

this is about as far as i got. Anyone that can help?

Best Regards,
Sandfordc
www.JavaScript-Central.tk


I am pretty sure that you can do what you want to do using regular
expression pattern matching. They are quite powerful, although setting up
more advanced matchings can be quite syntactically confusing.

Since you have been trying using only substring(), it sounds to me like
you might be unaware of the use of regular expressions. If this is the
case, try a google search with something like "javascript regular
expressions" and look through some of the examples.

steve

Jul 23 '05 #4
Sandfordc wrote:
I have tried several time to do this but have been unsucessful.
I tried something like:

myFunction(charater)

str=frm.s1.value
sb1=str.substring(0,charater)
sb2=str.substring(charater,str.length)
//looping function to find nearest space on left side
for(parameters){
sbs1=str.indexOf(' ')
}
//found the space on the right no problem
sb2=str.indexOf(' ')

this is about as far as i got. Anyone that can help?
Your pseudo-code seems to be trying to find the word(s) within a phrase
that contain a particular character.

The following turns a phrase into an array of 'words' (where a word
is a whitespace-delimited string of one or more non-whitespace
characters). It then removes every element in the word array that does
not contain the search string.

An empty string '' matches all words, ' ' (space or any other
whitespace character) does not match anything.
<script type="text/javascript">
function getWord(phrase, s){

// Create a regular expression from the search string
var re = RegExp(s);

// Remove extra white space and split text into an array
var p = phrase.replace(/\s+/g,' ').split(' ');
var i = p.length;

// Check to see if string matches some part of each word
while ( i-- ) {

// If no match, remove element from array
if ( ! re.test(p[i]) ) {
p.splice(i,1);
}
}

// Show the result
alert('There are ' + p.length + ' words that match ' + s
+ '\n' + p.join(', '));
}
</script>

<form action="">
<p>
String to match<br>
<input type="text" name="str" size="10"><br>
Phrase to search in&nbsp;&nbsp;
<input type="button" value="Search..." onclick="
getWord(this.form.phrase.value, this.form.str.value);
"><br>
<textarea name="phrase" cols="30" rows="20"></textarea>
</p>
</form>


Best Regards,
Sandfordc
www.JavaScript-Central.tk

--
Rob
Jul 23 '05 #5
Random wrote:
[...]
Regular Expressions can do this easily.

function leftWordfromChar( Line, Char ) {
if( ! Line || ! Char ) return null;
return Line.match(
RegExp( '\\b(\\S*' + Char + '\\S*)\\b', 'i' )
)[1];
}

[...]

Cute. Didn't think of that approach - not sure about the use of \S,
seems to me \w is a better flat as it will split words on characters
such as colons, semi-colons, hyphens, etc. that are not matched by
\S. But I guess we're just guessing...

All the matching words can be extracted using:

function leftWordfromChar( Line, Char ) {
if( ! Line || ! Char ) return null;
var re = new RegExp('\\b(\\w*'+Char+'\\w*)\\b','g')
return Line.match(re).join(' : ' );
}

If hyphenated words are to be treated as one word:

var re = new RegExp('\\b([\\w|-]*'+Char+'[-|\\w]*)\\b','g')

and so on...
--
Rob
Jul 23 '05 #6
RobG wrote:
Random wrote:
[...]
Regular Expressions can do this easily.

function leftWordfromChar( Line, Char ) {
if( ! Line || ! Char ) return null;
return Line.match(
RegExp( '\\b(\\S*' + Char + '\\S*)\\b', 'i' )
)[1];
}

[...]

Cute. Didn't think of that approach - not sure about the use of \S,
seems to me \w is a better flat as it will split words on characters
such as colons, semi-colons, hyphens, etc. that are not matched by
\S. But I guess we're just guessing...

All the matching words can be extracted using:

function leftWordfromChar( Line, Char ) {
if( ! Line || ! Char ) return null;
var re = new RegExp('\\b(\\w*'+Char+'\\w*)\\b','g')
return Line.match(re).join(' : ' );
}

If hyphenated words are to be treated as one word:

var re = new RegExp('\\b([\\w|-]*'+Char+'[-|\\w]*)\\b','g')

and so on...
--
Rob

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.

Jul 23 '05 #7
"Random" <ra*******@gmail.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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #8
Yeah I have never heard of RegExp().
Could someone please explain the syntax?

Thanks

Jul 23 '05 #9
Sandfordc wrote:
Yeah I have never heard of RegExp().
Could someone please explain the syntax?

Thanks


Regular Expressions are a powerful pattern matching tool in any
programmer's toolbox, allowing you search for patterns of characters as
opposed to fixed strings.

I learned to use them in PERL, but some PERL RE functionality is not
supported in JavaScript.

See
http://msdn.microsoft.com/library/en...xpressions.asp

And
http://msdn.microsoft.com/library/en...gexpsyntax.asp

Most people I've spoken to found them cryptic and unintuitive when they
first started using them. Feel free to pose any specific questions you
may have after reading the above, or email me individually if you'd
prefer.

One thing: don't let yourself be tempted to over-use them. Much of the
time you can get by with less complex search engines, such as
..indexOf() and .lastIndexOf() . The regexp engine can be comparatively
slow as it backtracks repeatedly, often byte by byte.

Jul 23 '05 #10
Lasse Reichstein Nielsen wrote:
"Random" <ra*******@gmail.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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Excellent points.

Curious, I checked it out. In my example of leftWordfromChar():
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
leftWordfromString.

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.

Jul 23 '05 #11
Random wrote:
Perhaps a better definition of a 'word' would be:
\w*(\w+[-']{1})*\w+


The {1} is redundant.

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jester.dev | last post: by
10 posts views Thread by Case Nelson | last post: by
13 posts views Thread by Nige | last post: by
5 posts views Thread by JrMc | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.