Solved

JavaScript: First Word that Starts with "qu"

Posted on 2012-03-26
6
325 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 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
Industry Leaders: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
DataTable column sorting incorrectly 2 36
setInterval() calls function twice in one interval 2 51
What kind of script/language created this graph? 6 65
Diff of the day 2 41
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

734 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