Solved

JavaScript: First Word that Starts with "qu"

Posted on 2012-03-26
6
319 Views
Last Modified: 2012-06-21
Using JavaScript, how can I get the first word in a string that starts with "qu"?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript">

var str='The duck quacked quietly';

alert( first_qu(str) );  // Should be "quacked"

function first_qu(str) {
 return str;
}

</script>
</head>
<body>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 450 total points
ID: 37766238
http://jsfiddle.net/mNQet/1/
//'The duck quacked quietly')
    var str='The duck quacked quietly';

alert( first_qu(str) );  // Should be "quacked"

function first_qu(str) {
 return /qu[^(qu) ]*/.exec(str);
}

Open in new window

0
 
LVL 5

Assisted Solution

by:Ronak Patel
Ronak Patel earned 50 total points
ID: 37766298
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript">

var str='The duck quacked quietly';

alert( first_qu(str) );  // Should be "quacked"

function first_qu(str) 
{
	var arr = str.split(" ");
	var startWithQU = new Array();
	var index = 0;
	for (i = 0; i < arr.length; i++)
	{
		if(arr[i].substr(0,2) == "qu")
		{
			startWithQU[index] = arr[i];
			index++;
		}
	}
	
	return startWithQU[0];
}

</script>
</head>
<body>

</body>
</html>

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 37766586
Oops, I may have accepted an answer too soon.  There is a problem:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript">

var str='quackquack hello world';
alert( first_qu(str) );  // Should be "quackquack"

var str='Hello quackquachk 123';
alert( first_qu(str) );  // Should be "quackquachk"

function first_qu(str) {
 return /qu[^(qu) ]*/.exec(str);
}

</script>
</head>
<body>

</body>
</html>

Open in new window

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 16

Author Comment

by:hankknight
ID: 37766610
This works:


function first_qu(str) {
 return /qu[^W) ]*/.exec(str);
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 37766627
Remove (qu) in the regex,  I'm not at my desk ...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37766744
http://jsfiddle.net/mNQet/2/

function first_qu(str) {
 return /qu[^ ]*/.exec(str);
}

Open in new window

¿
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question