Jquery 将匹配的字符串包装在单独的<;标签>;

Jquery 将匹配的字符串包装在单独的<;标签>;,jquery,Jquery,我在div中有一个字符串和一个输入框,用户可以在其中键入值。我想要的是,如果用户在中键入“some”,因为字符串已经包含“some”,我想将该“some”放在不同的中,就像这是somestring 这是一些字符串! 我试图使用indexOf()在中识别匹配的userinput字符串,但不确定是否正确。下面是小提琴链接(不完整的小提琴)。需要关于如何完成此操作的提示 使用替换功能: str_to_replace = 'some'; current_string = 'this is some s

我在div中有一个字符串和一个输入框,用户可以在其中键入值。我想要的是,如果用户在
中键入“some”,因为字符串已经包含“some”,我想将该“some”放在不同的
中,就像
这是somestring

这是一些字符串!
我试图使用
indexOf()
中识别匹配的userinput字符串,但不确定是否正确。下面是小提琴链接(不完整的小提琴)。需要关于如何完成此操作的提示

使用替换功能:

str_to_replace = 'some';
current_string = 'this is some string!';
replaced_string = current_string.replace(str_to_replace, '<i>' + str_to_replace + '</i>');
str_to_replace='some';
current_string='这是一些字符串!';
替换字符串=当前字符串。替换(str_to_replace,“+str_to_replace+”);
如果要防止多次包装字符串,请使用:

str_to_replace = 'some';
current_string = 'this is some string!';

// check if we already have wrapped str_to_replace in the i tag
if( current_string.indexOf('<i>' + str_to_replace  + '</i>') == -1 )  {
    replaced_string = current_string.replace( str_to_replace, '<i>' + str_to_replace + '</i>' );
}
str_to_replace='some';
current_string='这是一些字符串!';
//检查我们是否已经在i标签中包装了要替换的str_
if(当前字符串索引of(“”+str\u to_replace+“”)=-1){
替换字符串=当前字符串。替换(str_to_replace,“+str_to_replace+”);
}
使用替换功能:

str_to_replace = 'some';
current_string = 'this is some string!';
replaced_string = current_string.replace(str_to_replace, '<i>' + str_to_replace + '</i>');
str_to_replace='some';
current_string='这是一些字符串!';
替换字符串=当前字符串。替换(str_to_replace,“+str_to_replace+”);
如果要防止多次包装字符串,请使用:

str_to_replace = 'some';
current_string = 'this is some string!';

// check if we already have wrapped str_to_replace in the i tag
if( current_string.indexOf('<i>' + str_to_replace  + '</i>') == -1 )  {
    replaced_string = current_string.replace( str_to_replace, '<i>' + str_to_replace + '</i>' );
}
str_to_replace='some';
current_string='这是一些字符串!';
//检查我们是否已经在i标签中包装了要替换的str_
if(当前字符串索引of(“”+str\u to_replace+“”)=-1){
替换字符串=当前字符串。替换(str_to_replace,“+str_to_replace+”);
}
$(文档).就绪(函数(){
var替换了U字符串;
变量stru__替换;
无功电流_串;
$('#text_inp').keyup(函数(){
str_to_replace=$('#text_inp').val();
当前_字符串=$('#div_test')。text();
var regex=new RegExp('\\b('+str_to_replace+')\\b,'gim');
替换的字符串=当前字符串。替换(regex,$1');
$('#div_test').html(替换为_字符串)
});
});
html代码

 <div id="div_test">this is some string!

Ever needed custom formatted sample / test data, like, bad? Well, that's the idea of this script. It's a free, open source tool written in JavaScript, PHP and MySQL that lets you quickly generate large volumes of custom data in a variety of formats for use in testing software, populating databases, and... so on and so forth.

This site offers an online demo where you're welcome to tinker around to get a sense of what the script does, what features it offers and how it works. Then, once you've whet your appetite, there's a free, fully functional, GNU-licensed version available for download. Alternatively, if you want to avoid the hassle of setting it up on your own server, you can donate $20 or more to get an account on this site, letting you generate up to 5,000 records at a time (instead of the maximum 100), and let you save your data sets. Click on the Donate tab for more information. 


</div>

<input  type="text" id="text_inp"/>
这是一些字符串!
是否需要自定义格式的样本/测试数据,例如,坏?嗯,这就是这个剧本的想法。这是一个用JavaScript、PHP和MySQL编写的免费开源工具,可以让您以各种格式快速生成大量自定义数据,用于测试软件、填充数据库和。。。诸如此类。
本网站提供了一个在线演示,欢迎您在其中进行修补,了解脚本的功能、功能以及工作方式。然后,一旦你有了胃口,就可以下载一个免费的、功能齐全的、GNU许可的版本。或者,如果你想避免在自己的服务器上设置它的麻烦,你可以捐赠20美元或更多在这个网站上获得一个帐户,让你一次最多生成5000条记录(而不是最多100条),并让你保存数据集。单击“捐赠”选项卡了解更多信息。
$(文档).就绪(函数(){
var替换了U字符串;
变量stru__替换;
无功电流_串;
$('#text_inp').keyup(函数(){
str_to_replace=$('#text_inp').val();
当前_字符串=$('#div_test')。text();
var regex=new RegExp('\\b('+str_to_replace+')\\b,'gim');
替换的字符串=当前字符串。替换(regex,$1');
$('#div_test').html(替换为_字符串)
});
});
html代码

 <div id="div_test">this is some string!

Ever needed custom formatted sample / test data, like, bad? Well, that's the idea of this script. It's a free, open source tool written in JavaScript, PHP and MySQL that lets you quickly generate large volumes of custom data in a variety of formats for use in testing software, populating databases, and... so on and so forth.

This site offers an online demo where you're welcome to tinker around to get a sense of what the script does, what features it offers and how it works. Then, once you've whet your appetite, there's a free, fully functional, GNU-licensed version available for download. Alternatively, if you want to avoid the hassle of setting it up on your own server, you can donate $20 or more to get an account on this site, letting you generate up to 5,000 records at a time (instead of the maximum 100), and let you save your data sets. Click on the Donate tab for more information. 


</div>

<input  type="text" id="text_inp"/>
这是一些字符串!
是否需要自定义格式的样本/测试数据,例如,坏?嗯,这就是这个剧本的想法。这是一个用JavaScript、PHP和MySQL编写的免费开源工具,可以让您以各种格式快速生成大量自定义数据,用于测试软件、填充数据库和。。。诸如此类。
本网站提供了一个在线演示,欢迎您在其中进行修补,了解脚本的功能、功能以及工作方式。然后,一旦你有了胃口,就可以下载一个免费的、功能齐全的、GNU许可的版本。或者,如果你想避免在自己的服务器上设置它的麻烦,你可以捐赠20美元或更多在这个网站上获得一个帐户,让你一次最多生成5000条记录(而不是最多100条),并让你保存数据集。单击“捐赠”选项卡了解更多信息。

当用户再次键入单词some??将再添加一个li标签。对吗???@ShriyaR谢谢,我已经更新了上面的代码situation@saqibamin如果div包含多个some word,则此代码是否替换所有some word?否,它将仅替换一个发生的情况当用户再次键入some word时会发生什么情况??将再添加一个li标签。对吗???@ShriyaR谢谢,我已经更新了上面的代码situation@saqib如果div包含多个some单词,那么这个代码是否会替换所有some单词?不,它将只替换一个courrencehi Shriya,谢谢代码。很好!只是好奇“\\b”,我从哪里得到“\b”是匹配结尾或beinging处的字符,但是额外的“\”有什么用呢?\Hi Shriya,谢谢你的代码。很好!只是好奇“\\b”,我从哪里得到“\b”是匹配结尾或beinging的字符,但是额外的“\”有什么用呢\