为了账号安全,请及时绑定邮箱和手机立即绑定

JS match() 方法?

JS match() 方法?

九州编程 2019-02-08 11:07:04
<html><body><script type="text/javascript">var str="Hello world!"document.write(str.match("world") + "<br />")document.write(str.match("World") + "<br />")document.write(str.match("worlld") + "<br />")document.write(str.match("world!"))</script></body></html>-----------------------------------------------结果为何是这样!worldnullnullworld!
查看完整描述

2 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。
语法:
stringObject.match(searchvalue)
stringObject.match(regexp)

参数描述:

searchvalue 必需。规定要检索的字符串值。
regexp 必需。规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp 对象,则需要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。
例子:
<script type="text/javascript">
var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))
</script>

输出:
world
null
null
world!



查看完整回答
反对 回复 2019-03-26
  • 2 回答
  • 0 关注
  • 502 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信