Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me with my code, I want to the value to be matched in the variable myString but it is not working:

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

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

You can use regular expression, for matching of string using the object called RegExp:

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

This will help you to ensure that it matches the characters which are end with "od".

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java Expert? Join Java Course now!!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 15, 2021 in Java by dante07 (13.1k points)

Browse Categories

...