473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1974
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
10
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no...
13
by: Nige | last post by:
To save me re-inventing the wheel, does anyone have a function to capitalise each word in form data? I was thinking along the lines of capitalise the first letter of the string, then any other...
3
by: gdogg1587 | last post by:
Greetings. I'm working on a program that will "descramble" words. Think of a word scramble game where there is a list of characters, and several blank spaces to denote the word(s) that you are...
5
by: JrMc | last post by:
I am new to this .net environement. I'm needing to do a simple task - I need to read the first character of a word and replace it with an uppercase. Example: "horse" --> change to "Horse" ...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
0
by: raypjr | last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
2
by: whodgson | last post by:
The following code accepts a string from the user and swaps the first and last letters in each word. It prints out the original string incorrectly by dropping off the first letter of the first word....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.