3

I have this code here:

<!DOCTYPE html>
<html>
<body>

<p id="demo">Display the result here.</p> 
<input type = "text" value = "ood" id = "txt1"/>
<script>


var myString = "How much wood could a wood chuck chuck";
var myWord = document.getElementById("txt1").value;   // ood
var myPattern = /.myWord/;        // I want this to be /.ood/
var myResult = myString.match(myPattern);

document.getElementById("demo").innerHTML = myResult;

</script>

</body>
</html>

Now what I want to do here is that I want the value of the txt1 which is ood to be matched in myString.

But putting the variable myWord into the myPattern like this: /.myWord/ won't work. Need help please. Many thanks

UPDATE:

I did everything as what it is in the answers but it returns wood,wood instead of wood only, I just wanted to get only 1 match. Just like using for example /.ood/ - this only returns 1 match. Please help

And also, how can I get the word wood by having only od in my input text. I just wanted this for searching..

1
  • 2
    Use RegExp constructor syntax as var myPattern = new RegExp('\.' + myWord, 'gi'); Commented Sep 7, 2015 at 3:21

2 Answers 2

3

You can use a string as a regular expression using the RegExp Object:

var myPattern = new RegExp('.'+myWord,'g'); 

Fiddle Example

Doing a single match in your case, is simply changed the second parameter in RegExp from g to m (which means to make one match per line for multi lines, but in this case a strings is simply all one line). For finding the word "wood" from "ood","od","d", or other cases. You can do this:

var myPattern = new RegExp("\\b\\w+"+myWord+"\\b",'m'); 

Note I have a solution in the comments below, but this one is better.

The items \\b ... \\b mean word boundaries. Basically ensuring that it matches a single word. Then the \\w means any valid "word character". So overall the regexp means (using myWord = "od"):

|word boundary| + (1 or more word characters) + "od" + |word boundary|

This will ensure that it matches any words in the string than end with the characters "od". Or in a more general case you can do:

var myPattern = new RegExp("\\b\\w*"+myWord+"\\w*\\b",'m'); 
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks it worked and actually found the match but what i just wanted is to get only 1 match. Thanks.
@wwwDELL In that case you can use RegExp('.'+myWord,'m'); to get a single match in this case. The m means to do a match for each line.
Thanks! it really worked. I also updated my question. when the value of the input text is od it supposed to find wood but it could'nt match any word from that of myString :(
@wwwDELL Edited my comment above with why that happens. So for example you can fix that by doing new RegExp('.{1,2}'+myWord,'g')., meaning you can have 1 to 2 characters before od.
@wwwDELL Yes there is, but it has some overhead. You can split the string up by it's words. Then use new RegExp(".+"+myWord+"$",'g'); to mean: 1 or more characters + ends with "od" on each word. Here is an example. It's possible there is a better way to do that.
|
2

Create a Regexp object like

new RegExp('.ood','g');

like in

var searchstring='ood' // this is the one you get in a variable ...

var myString = "How much wood could a wood chuck chuck";
var myPattern=new RegExp('.'+searchstring,'gi');
var myResult = myString.match(myPattern);

5 Comments

would you site some example please?
The string ood should be dynamic from the textbox value, see my comment above
You also need to escape . by preceding \, in regex
Well, yes, then do that too ;-) BuI I understood that OP actually wanted to look for an "arbitrary character" not the actual dot.
Thanks it worked and actually found the match but what i just wanted is to get only 1 match. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.