Solved

JavaScript: First Word that Starts with "qu"

Posted on 2012-03-26
6
311 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now