Skip to content

正则的作用 #27

Open
Open

Description

@Yangxiaqing

正则:
它就是一个规则,用来处理字符串的一个规则(正则就是用来处理字符串的)

处理(作用):

  • 匹配——判断一个字符串是否符合我们制定的规则——test
    reg.test(str)
var reg = /\d/;// 包含一个0-9之间的数字
console.log(reg.test('yang'));//false
console.log(reg.test('1'));//false
console.log(reg.test('yang2017'));//true
  • 捕获——把字符串中符合我们正则规则的内容捕获到——exec
    reg.exec(str)
var reg = /\d/;
console.log(reg.exec('yang'));//null
console.log(reg.exec('1'));//["1", index: 0, input: "1"]

如何创建一个正则?

  • 字面量方式:
var reg = /\d/;
  • 实例创建方式:
var reg = new RegExp(" ");
  • 正则的两种创建方式是有区别的。

如何学习正则:

console.dir(RegExp.prototype);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions