?
Solved

JavaScript: First Word that Starts with "qu"

Posted on 2012-03-26
6
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1800 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

764 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