想法:使用该单词中的一个字母从字符串中检索单词 [英] Idea: Retrieve a Word from a string using one of the letters in that word

查看:81
本文介绍了想法:使用该单词中的一个字母从字符串中检索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几次这样做但是没有成功。

我试过类似的东西:


myFunction(charater)


str = frm.s1.value

sb1 = str.substring(0,charater)

sb2 = str.substring(charater,str。长度)

//循环函数找到左边最近的空间

for(参数){

sbs1 = str.indexOf('' '')

}

//发现右边的空格没问题

sb2 = str.indexOf('''')


这是我所得到的。有人可以提供帮助吗?


最诚挚的问候,

Sandfordc
www.JavaScript-Central.tk

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

推荐答案

Sandfordc说:
Sandfordc said:

我已经尝试了几次这样做但是没有成功。
我尝试了类似的东西:

myFunction(charater)

str = frm.s1.value
sb1 = str.substring(0,charater)
sb2 = str.substring(charater,str.length)

/ / looping函数找到左边最近的空间
for(parameters){
sbs1 = str.indexOf('''')
}

//找到右边的空间没有问题
sb2 = str.indexOf('''')

这就是我所知道的。有人可以提供帮助吗?

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.


Sandfordc写道:
Sandfordc wrote:
我已经尝试了几次这样做但是没有成功。
我尝试了类似的东西:

myFunction(charater)

str = frm.s1.value
sb1 = str.substring(0,charater)
sb2 = str.substring(charater,str.length)

/ / looping函数找到左边最近的空间
for(parameters){
sbs1 = str.indexOf('''')
}

//找到右边的空间没有问题
sb2 = str.indexOf('''')

这就是我所知道的。谁能提供帮助?

最诚挚的问候,
Sandfordc
www.JavaScript-Central.tk
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



如果我说得对,你试图找到包含
$ b $的最左边的单词ba提供角色。


正则表达式可以轻松完成。


函数leftWordfromChar(Line,Char){

if(!Line ||!Char)返回null;


返回Line.match(

RegExp(''\\b(\ \ S *''+ Char +''\\ S *)\\ b'',''我')

)[1];


}


警报(leftWordfromChar('嘿,看看我的鞋!'',''e''));


会发出警告:''嘿''

这是不区分大小写的。为了区分大小写,请从

RegExp(''\\b()中删除

,'i''

\\ S *''+ Char +''\\ S *)\\ b'',''我')


你可以考虑制作这是你的功能选择。


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.


2005年6月1日星期三21:01:58 -0700,Sandfordc写道:
On Wed, 01 Jun 2005 21:01:58 -0700, Sandfordc wrote:
我已经尝试过几次这样做但是没有成功。
我尝试了类似的东西:

myFunction(字符)

str = frm .s1.value
sb1 = str.substring(0,charater)
sb2 = str.substring(charater,str.length)

//循环函数找到最近的空格在左侧
for(参数){
sbs1 = str.indexOf('''')
}

//找到右边的空格没问题
sb2 = str.indexOf('''')

这就是我的意思。谁能提供帮助?

最诚挚的问候,
Sandfordc
www.JavaScript-Central.tk
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




我很确定你可以用常规做你想做的事

表达式匹配。它们非常强大,虽然设置

更高级的匹配可能会在语法上造成混乱。


因为你一直在尝试只使用substring(),它听起来对我来说就像

你可能不知道使用正则表达式。如果这是

的情况,请尝试使用类似javascript regular

表达式的谷歌搜索并查看一些例子。


史蒂夫



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


这篇关于想法:使用该单词中的一个字母从字符串中检索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆