问题描述
您好专家,
我有一个简单的文本行,其中包含两个带有空格的单词.我正在努力使正则表达式相同.
过去,当我有一个单词与一行匹配时,我执行了以下操作,而且是无缝的..
Hello expert,
I have a simple text line contains two words with a blank space. I am struggling to make regular expression for the same.
In past, when I have single word to match with a single line, i did the following and it was seamless..
var inparam = document.getElementById("inparam").value;
var inputuser = inparam;
var re = new RegExp(inputuser,"g");
var matchtext = re;
var result = matchtext.test(line);
alert(result);
如您所见,我正在获取一个动态字符串来与该行匹配.在我的示例中,变量"inparam"从名为inparam的元素获取一个值.我用"re"为正则表达式类创建了一个对象,然后将其匹配.
让我们来line ="Hello World"
inparam ="Hello"
现在考虑我从不同元素接收两个带有空格的单独值,那么我的对象应该是什么样子?
让我们来line ="Hello World"
firstin ="Hello"
secondin =世界"
我已经尝试过了,但是没有用..
As you can see, I am getting a dynamic string to match with the line. In my example, the variable "inparam" taking a value from the element named inparam. I have made an object to regular expression class with "re", and then I am matching it.
Let, line="Hello World"
inparam="Hello"
Now consider I am receiving two separate values from different elements with blank space, then what should be my object look like??
Let, line="Hello World"
firstin="Hello"
secondin="World"
I have tried this but didn''t worked..
var firstin = document.getElementById("firstin").value;
var secondin = document.getElementById("secondin").value;
var inputfirst = firstin;
var inputsecond = secondin;
var re = new RegExp(inputfirst+"\s"+inputsecond,"g");
var matchtext = re;
var result = matchtext.test(line);
alert(result);
有任何帮助或提示吗?
Any help or hint please???
推荐答案
var re = new RegExp('(' + inputfirst + '|' + inputsecond + ')',"g");
这篇关于Java脚本正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!