Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

JavaScript: First Word that Starts with "qu"

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

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hankknight

ASKER

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

This works:


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

Open in new window

Remove (qu) in the regex,  I'm not at my desk ...
http://jsfiddle.net/mNQet/2/

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

Open in new window

¿