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

S

Sandfordc

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
 
L

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

Random

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?

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

coolsti

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
 
R

RobG

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?

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) ) {
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>
 
R

RobG

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

Random

RobG said:
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...


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

Lasse Reichstein Nielsen

Random said:
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
 
S

Sandfordc

Yeah I have never heard of RegExp().
Could someone please explain the syntax?

Thanks
 
R

Random

Sandfordc said:
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-us/script56/html/js56reconIntroductionToRegularExpressions.asp

And
http://msdn.microsoft.com/library/en-us/script56/html/js56jsgrpregexpsyntax.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.
 
R

Random

Lasse said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top