0

I am a PHP coder and Javascript is new to me. Now I am reading some manuals about Regexp in Javascript. I can't do it work with jQuery.

Here are 2 examples.

http://jsfiddle.net/borayeris/utG6H/ This one is working:

<script type="text/javascript">
 $(function(){
     str = "For more information, see Chapter 3.4.5.1";
     re = /(chapter \d+(\.\d)*)/i;
     found = str.match(re);
    document.write(found);
 });
</script>

http://jsfiddle.net/borayeris/QyFPE/1/ This one not:

<script type="text/javascript">
 $(function(){
     str = "For more information, see Chapter 3.4.5.1";
     re = /(chapter \d+(\.\d)*)/i;
     found = str.match(re);
     $('div#write3').text(found);
 });
 </script>

The markup:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RegEx</title>
<script type="text/javascript" src="jquery-1.4.4.js"></script>
</head>
<body>

<div id="write3"></div>

</body>
</html>
3
  • There is a box for JavaScript code so you don't have to place a script tag in the HTML code box...
    – BoltClock
    Dec 29, 2010 at 19:23
  • Jquery in only a javascript framework, for regexp you have to use javascript only. Javascript regexp are a little different from php regexp (but are similiar). Search on google...
    – albanx
    Dec 29, 2010 at 19:25
  • This is not your problem but: you didn't declare your variables as locals, therefore they will be global (and global variables are baaaad). You should write var str = ... . Dec 29, 2010 at 19:40

4 Answers 4

5

Found is an array of matches (or null if nothing is found). Try changing this: $('div#write3').text(found); to this $('div#write3').text(found[0]);

See here for more information.

1
  • Also, regular expressions are not jQuery specific, they're built into JavaScript. Dec 29, 2010 at 19:28
3

Drackir is correct, but misses the more interesting point in the difference between your working and not working examples. document.write expects a string, and the toString method of the array is called automatically. jQuery's text can take a string or a function, so the array's toString is not automatically called.

The truly parallel examples are:

document.write(found); 

and

$('div#write3').text(found.toString());
1
  • Thanks. I actually couldn't get the first example to show up, so I just focused on getting the second one to work. Dec 29, 2010 at 21:01
1

http://jsfiddle.net/QyFPE/5/

  1. You don't need to place the header, doctype, etc. in a jsfiddle, the blocks put the code in the appropriate place.
  2. This should work, the only change I made was placing the content in the correct location(s) (and un-grouped your pattern for catching sub-group numerics in your chapter number)
3
  • Why $('div#write3').text('test'+found); works but $('div#write3').text(found); this doesn't?
    – borayeris
    Dec 29, 2010 at 19:47
  • @boyayeris - because JS loose typing means that a string plus an array causes toString to be called on the array and then the result is appended to the original string.
    – jball
    Dec 29, 2010 at 19:49
  • Because found is not a string, it's an array. Placing it as a concatenation forces javascript to cast it to a string (effectively dumping the objects out, comma-delimited). ''+found should also work, or (as @Drackir pointed out) using it's Nth element will work as well. Dec 29, 2010 at 19:49
1

The dom function document.write will accept an array name as parameter and can print all the elements

eg: <script language="JavaScript">
<!--
var myarray = new Array();
for (i = 0; i < 10; i++)
{
    myarray[i] = i;
}
document.write(myarray);
//  -->
</script>

will give 0,1,2,3,4,5,6,7,8,9

Where as .text() function can only accept a string as parameter 
text( textString )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.