正斜杠在JavaScript正则表达式中意味着什么? [英] What does the forward slash mean within a JavaScript regular expression?

查看:822
本文介绍了正斜杠在JavaScript正则表达式中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到关于 / 在JavaScript正则表达式中的含义的任何确切信息。

I can't find any definitive information on what / means in a JavaScript regex.

代码替换(/ \r / g,'');

我能弄清楚的是:


  • / =我不知道

  • \ r =回车

  • / g =我不知道,但这可能意味着比赛必须在上一场比赛结束时发生。

  • / = I don't know
  • \r = carriage return
  • /g = I don't know but It may mean 'the match must occur at the point where the previous match ended.'

推荐答案

斜杠表示正则表达式的开头和结尾。

The slashes indicate the start and end of the regular expression.

最后的 g 是一个标志,表示它是全局搜索。

The g at the end is a flag and indicates it is a global search.

来自文档


正则表达式有四个可选标志,允许全局和
不区分大小写的搜索。要指示全局搜索,请使用g
标志。要指示不区分大小写的搜索,请使用i标志。要
表示多行搜索,请使用m标志。要执行粘性
搜索,从目标
字符串中的当前位置开始匹配,请使用y标志。这些标志可以单独使用,也可以按任意顺序一起使用
,并作为正则表达式的一部分包含在内。

Regular expressions have four optional flags that allow for global and case insensitive searching. To indicate a global search, use the g flag. To indicate a case-insensitive search, use the i flag. To indicate a multi-line search, use the m flag. To perform a "sticky" search, that matches starting at the current position in the target string, use the y flag. These flags can be used separately or together in any order, and are included as part of the regular expression.

要包含正则表达式的标志,使用以下语法:

To include a flag with the regular expression, use this syntax:



 var re = /pattern/flags;

这篇关于正斜杠在JavaScript正则表达式中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆