JavaScript正则表达式案例
- 案例一:座机号码验证
//座机号码验证 0913-5549400 010-12345678
var reg4 = /^\d{3,4}-\d{7,8}$/; //或者var reg4 = /^\d{4}-\d{7}|\d{3}-\d{8}$/;
console.log(reg4.test('0913-5549400')); //true
console.log(reg4.test('010-12345678')); //true
console.log(reg4.test('21244-641928641246')); //false
- 敏感词替换
<body>
<textarea name="" cols="30" rows="10"></textarea>
<button>提交</button>
<div>123</div>
<script>
var reg = /激情/;
var text = document.querySelector('textarea');
var btn = document.querySelector('button');
var div = document.querySelector('div');
btn.onclick = function() {
div.innerHTML = text.value.replace(/激情/g, '**');
}
</script>
</body>