javaScript RegExp对象问题
来源:百度知道 编辑:UC知道 时间:2024/07/04 09:31:28
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>正则表达式--邮件内容处理程序</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script>
function getResult(){
var str=document.getElementById('content').value;
var from=/From: (\w+@\w+[\.\w+]?)/;
var to=/To: (\w+@\w+\.[a-z]+)/;
var subject=/Subject: (.*)/;
var f=str.match(from);
f=RegExp.$1;
var t=str.match(to);
t =RegExp.$1;
var s=str.match(subject);
s =RegExp.$1;
document.getElementById('from').innerHTML=f;
document.getElementById('to
<HTML>
<HEAD>
<TITLE>正则表达式--邮件内容处理程序</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script>
function getResult(){
var str=document.getElementById('content').value;
var from=/From: (\w+@\w+[\.\w+]?)/;
var to=/To: (\w+@\w+\.[a-z]+)/;
var subject=/Subject: (.*)/;
var f=str.match(from);
f=RegExp.$1;
var t=str.match(to);
t =RegExp.$1;
var s=str.match(subject);
s =RegExp.$1;
document.getElementById('from').innerHTML=f;
document.getElementById('to
可能你根本没有明白正则表达式的匹配,看你下面这段语句:
var str=document.getElementById('content').value;
var from=/From: (\w+@\w+[\.\w+]?)/;
var to=/To: (\w+@\w+\.[a-z]+)/;
var subject=/Subject: (.*)/;
//通过上门的定义语句,str是一个字符串,from\to\subject是三个正则表达式
var f=str.match(from);
f=RegExp.$1;
//这两个语句:对用正则表达式from对字符串str进行匹配,如果匹配成功,正则表达式from里面括号里面的内容就依次赋予RegExp.$1~RegExp.$n,也就是说,这两个语句执行以后,f的值是str里面“From: ”后面的符合电子邮件规则(\w+@\w+[\.\w+]?)的一个地址。
var t=str.match(to);
t =RegExp.$1;
//和上面一样的道理,执行后t就是邮件的目标地址
var s=str.match(subject);
s =RegExp.$1;
//s为邮件主题
核心的地方就是,字符串的.match方法或修改全局变量RegExp的内容,match以后可以从RegExp里面获取需要的信息。