简体   繁体   中英

regex for a word in a paragraph

regex for a paragraph written and i want to search for a word in it.

i tried using .search but i am not getting the right answer eg :-

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)

in the above sentence if i have to find the word "knowledge". what regex can i use .

Actually i have made a form in dojo :-

name : text area address : text area tele. no : number txt area

submit button

now when i click on the submit button. the name address and tele no. fields should automatically get filled in a letter which goes as follows :-

Dear [name] ,

This is to inform you that blah blah blah blah. you can contact the undersigned. [tele no] ,[address]

  1. i have made the form. now i am trying to match the label's inner html and the word in the brackets.if it is the same then the value has to get stroed in the letter. kindly help to give a better idea

See this:

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

But as pointed by Ollie, this is pretty pointless. Considering you may want to replace the string knowledge, I have used replace function too:

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);
}

Based on your edit, see my new 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);

The following RegEx:

/knowledge/ig

I don't think you need a RegEx for this anyway:

string.indexOf('knowledge'); // Will give you the first index of the word

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM