0

正则表达式写的一段,我想在其中搜索一个词。

我尝试使用 .search 但我没有得到正确的答案,例如:-

科学(来自拉丁语 scientia,意为“知识”)是一项系统性的事业,它以关于宇宙的可测试解释和预测的形式构建和组织知识。 [1] 在更古老且密切相关的含义中(例如,在亚里士多德中找到)

在上面的句子中,如果我必须找到“知识”这个词。我可以使用什么正则表达式。

实际上我已经在道场中制作了一个表格:-

名称:文本区 地址:文本区 电话。no : number txt 区域

提交按钮

现在当我点击提交按钮时。姓名地址和电话号码。字段应自动填写如下字母:-

亲爱的[姓名],

这是为了通知你,等等等等。您可以联系以下签名人。[电话号码] ,[地址]

  1. 我已经做了表格。现在我正在尝试匹配标签的内部 html 和括号中的单词。如果它是相同的,那么值必须在字母中得到 stroed。请帮助提出更好的想法
4

2 回答 2

2

看到这个:

http://jsfiddle.net/2hNwL/1/

但正如奥利指出的那样,这是毫无意义的。考虑到您可能想要替换字符串知识,我也使用了替换功能:

var str = 'Science (from Latin scientia, meaning "knowledge") is a systematic enterprise that builds and organizes knowledge in the form of testable explanations and predictions about the universe.[1] In an older and closely related meaning (found, for example, in Aristotle)';

var match = str.match(/knowledge/gi);

if(match != null)
{
    for(var i = 0; i < match.length; i++)
        alert(match[i]);

    var replacedString = str.replace(/knowledge/gi,'egdelwonk');
    alert(replacedString);
}

根据您的编辑,请参阅我的新 jsfiddle:http: //jsfiddle.net/kPRDH/

var str = 'Dear [name] ,This is to inform you that blah blah blah blah. you can contact the undersigned. [tele no] ,[address]';


str = str.replace(/\[name\]/g,'TCM');
str = str.replace(/\[tele no\]/g,'9999999999');
str = str.replace(/\[address\]/g,'221B Bakers Street');

alert(str);
于 2012-10-08T17:00:42.090 回答
1

以下正则表达式:

/knowledge/ig

无论如何,我认为您不需要 RegEx:

string.indexOf('knowledge'); // Will give you the first index of the word
于 2012-10-08T16:58:30.557 回答