jQuery中的正则表达式

bor*_*ris 0 javascript regex jquery

我是一个PHP编码器,Javascript对我来说是新的.现在我正在阅读一些关于Javascript中Regexp的手册.我无法使用jQuery.

这是两个例子.

http://jsfiddle.net/borayeris/utG6H/这个工作正在:

<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>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/borayeris/QyFPE/1/这不是:

<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>
Run Code Online (Sandbox Code Playgroud)

标记:

<!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>
Run Code Online (Sandbox Code Playgroud)

Ric*_*kir 5

Found是一个匹配数组(如果没有找到则为null).尝试改变这个:$('div#write3').text(found); 对此$('div#write3').text(found[0]);

有关更多信息,请参见此处