I have tried several time to do this but have been unsucessful.
I tried something like:
myFunction(char ater)
str=frm.s1.valu e
sb1=str.substri ng(0,charater)
sb2=str.substri ng(charater,str .length)
//looping function to find nearest space on left side
for(parameters) {
sbs1=str.indexO f(' ')
}
//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 11 2000
Sandfordc said: I have tried several time to do this but have been unsucessful. I tried something like:
myFunction(cha rater)
str=frm.s1.val ue sb1=str.substr ing(0,charater) sb2=str.substr ing(charater,st r.length)
//looping function to find nearest space on left side for(parameters ){ sbs1=str.index Of(' ') }
//found the space on the right no problem sb2=str.indexO f(' ')
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.
Sandfordc wrote: I have tried several time to do this but have been unsucessful. I tried something like:
myFunction(char ater)
str=frm.s1.valu e sb1=str.substri ng(0,charater) sb2=str.substri ng(charater,str .length)
//looping function to find nearest space on left side for(parameters) { sbs1=str.indexO f(' ') }
//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 leftWordfromCha r( Line, Char ) {
if( ! Line || ! Char ) return null;
return Line.match(
RegExp( '\\b(\\S*' + Char + '\\S*)\\b', 'i' )
)[1];
}
alert( leftWordfromCha r( '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.
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(char ater)
str=frm.s1.valu e sb1=str.substri ng(0,charater) sb2=str.substri ng(charater,str .length)
//looping function to find nearest space on left side for(parameters) { sbs1=str.indexO f(' ') }
//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
Sandfordc wrote: I have tried several time to do this but have been unsucessful. I tried something like:
myFunction(char ater)
str=frm.s1.valu e sb1=str.substri ng(0,charater) sb2=str.substri ng(charater,str .length)
//looping function to find nearest space on left side for(parameters) { sbs1=str.indexO f(' ') }
//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
<input type="button" value="Search.. ." onclick="
getWord(this.fo rm.phrase.value , this.form.str.v alue);
"><br>
<textarea name="phrase" cols="30" rows="20"></textarea>
</p>
</form> Best Regards, Sandfordc www.JavaScript-Central.tk
--
Rob
Random wrote:
[...] Regular Expressions can do this easily.
function leftWordfromCha r( 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 leftWordfromCha r( 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
RobG wrote: Random wrote: [...] Regular Expressions can do this easily.
function leftWordfromCha r( 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 leftWordfromCha r( 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.
"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.'
Yeah I have never heard of RegExp().
Could someone please explain the syntax?
Thanks
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
directory) and count number of times any given word appears
in the text.
#!/usr/bin/python
|
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 more than
10 letters, and find all possible mutations that match a word in my
dictionary (80k words). However a wildcard letter '?' is also an...
|
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 letter after a
space or hyphen.
So many users seem to think that just because they are online they can
use names like "mr john smith" and so I have...
|
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 to come up with from the list.
i.e.(* denotes a space): _ _ _ _ _ * _ _ _ _ _ _
characters: .hwodlelrlo
answer: hello world.
|
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"
I can do this on an as/400 using RPG, but cannot figure out the syntax to do
it in .net with the .substring and .toupper methods.
--
| |
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 execute a MailMerge to create mailing labels or
customized letters.
A label name known to MS Word MailMerge mailing label wizard may be used or
a...
|
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 appreciated.Here is the code to give more detail:
Dim GameOver As Boolean
Dim NumWords As Integer, ThreeWordList(1000) As String,...
|
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 this: can Access create
the document and place it as an OLE object to the relevant table?
Any help is greatly appreciated.
Ricky
|
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. I would like to establish what error in the code is causing the first word to be mangled.
#include<iostream>
#include<cstring>
#include<cstdlib>...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |