Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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..


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


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..

C#
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???
Posted
Comments
Sergey Alexandrovich Kryukov 18-Oct-12 0:48am    
Here is a hint: "did't work" is not informative.
--SA
comred 18-Oct-12 1:15am    
Thankx as I expected frm you...
enhzflep 29-Oct-12 15:58pm    
Yet still you offered such a woeful explanation of the problem?
Have a look for some different Regexs. It's actually not clear what your intention.

Is it your intention to match _either_ "Hello" or "World", or is it your intention to match "Hello World"?

In the first case, you need to check twice to see if it matches either input.
In the second case, you need to stick em together with a space in the middle then do a single check on that.

Give better description, get better help!

1 solution

try
JavaScript
var re = new RegExp('(' + inputfirst + '|' + inputsecond + ')',"g");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900