您的位置:首页 > Web前端 > JavaScript

JavaScript 中匹配单个字符实例

2017-08-10 16:15 405 查看
通过正则表达式,匹配任意大写“L”实例:

代码如下:

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script>

var myRegExp=/L/;

function Validate(entry){

return myRegExp.test(entry);
}
function ShowPrompt(){
var entry=prompt("This script tests for matches for the regular expression pattern:"+myRegExp+".\nType in a string and clik on the OK button.","Type your text here.");
if(Validate(entry)){
alert("There is a match!\n The regular expression pattern is:"+myRegExp+".\n");
}else{
alert("There is no match in the string you entrered.\n"+myRegExp+".\n The string that you entere was:'"+entry+"'.");
}
}
</script>
</head>
<body>
<form name="myFrom">
<button type="button" onclick="ShowPrompt()">Click here to enter text.</button>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: