在句子中查找确切的字符串 [英] Find Exact String of Characters in Sentence
问题描述
我正在尝试为聊天程序替换字符串中的笑脸字符,并将其替换为正确的图像.我以为我可以使用它,但前提是它是字符串中唯一的字符.我怎样才能找到一个句子中的字符,有 也完全匹配.
I'm trying to replace smiley characters in a string for my chat program and replace them with the correct image. I thought I had it working but only if it is the only characters in the string. How can I find the characters in a sentence, has to match exactly too.
这是我当前的正则表达式
This is my current regex
str1 ="^ \< \:\-O $";
var re = new RegExp(str1,"g");
cleanmsg = cleanmsg.replace(re,'< img类=笑脸" src =" smileys/'+ smileys [index] +'.png''alt ='+ index +'">)')
str1 = "^\<\:\-O$";
var re = new RegExp(str1, "g");
cleanmsg = cleanmsg.replace(re, '<img class="smiley" src="smileys/'+smileys[index]+'.png" alt="'+index+'">')
我想做的事的例子:
测试句子:-)在这里找到多个:-)
test sentence :-) find multiple :-) here
返回
测试语句< img>查找多个< img>这里
test sentence <img> find multiple <img> here
推荐答案
那是JavaScript-这是一个.NET-论坛...
that's JavaScript - this is a .NET - Forum...
无论如何,使用^可以在输入的开头搜索字符串,而
Whatever, with ^ you search for a string at the beginning of the input and
则是输入的结尾.从图案中删除这些字符.
is the end of the input. Remove these characters from your pattern.
我不知道您与其余\\< \:-O匹配什么,但是如果它是唯一的字符,如果它与您想要的匹配,那么就没有^和
I don't know, what you match with the rest \<\:-O, but if it matches what you want if it's the only characters, then without ^ and
它匹配任何句子中出现的任何内容.
it matches any occurence in any sentence.
问候,
这篇关于在句子中查找确切的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!