10

I want to find words which start with a specific letter in a string using the following code. The specific letter would be supplied by the user in a text box.

This is what I have:

<!DOCTYPE html>
<html>
<body>

<input id="srch" type="text" />
<button onClick=searchword()>Search</button>

<p id="wrd" > hallo, this is a test john doe .
Another tea house pole.
</p>

</body>

<script>

function searchword() {

var s = document.getElementById("wrd").innerHTML;
var p= document.getElementById("srch").value;

var regx = new RegExp("(?:^|\W)" + p + "(\w+)(?!\w)","gi");

var re = regx, match, matches = [];

while (match = re.exec(s)) {
  matches.push(match[0]);
}
alert(matches);


}
</script>
</html>
tzaman
  • 46,925
  • 11
  • 90
  • 115
codesam
  • 105
  • 1
  • 1
  • 4
  • 1
    Use regexp `\bW[^\b\s]+\gi ` to find words starting on "W" or "w". – Mateusz Nowak May 05 '15 at 07:30
  • How do I insert the first letter of the word which is supplied by the user i.e. 'W' in your example in the regular expression? , – codesam May 05 '15 at 08:14
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp – Mateusz Nowak May 05 '15 at 08:16
  • I got the following answer and its working,thanks var regx = new RegExp("(\\b"+p+"\\S+\\b)","ig"); – codesam May 05 '15 at 08:40
  • Possible duplicate of [Javascript : Extract words starting with specific character in a string](https://stackoverflow.com/questions/28739496/javascript-extract-words-starting-with-specific-character-in-a-string) – Liam Jun 21 '17 at 10:25

2 Answers2

13

You can use word boundaries \b, the following example shows how to match every word starting with t

var string ="hallo, this is a test john doe .Another tea house pole. Hey Tom."
result = string.match(/(\bt\S+\b)/ig);
//result = string.match(/(\st\S+)/ig); // alternative
document.write(result);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • How do I insert the first letter of the word which is supplied by the user i.e. 't' in your example in the regular expression? , – codesam May 05 '15 at 08:13
  • I got the following answer and its working, thanks var regx = new RegExp("(\\b"+p+"\\S+\\b)","ig"); – codesam May 05 '15 at 09:05
  • Not working when there are special characters before the letter "t". For example it will match a work like "étonnant". – Kr1 Nov 03 '20 at 12:55
  • @Liam You can also try: `result = string.match(/(\st\S+)/ig);` to avoid false matches like `tonnant` – Pedro Lobito Nov 06 '20 at 20:41
-1
var string ="hallo, this is a test john doe .Another tea house pole. Hey 
Tom."
result = string.match(/(\ba\S+\b)/ig);
document.write(result);
  • 1
    Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jun 25 '22 at 13:51