使用node.js的带有数字的字符串的正则表达式

时间:2022-12-19 18:27:10

I am using replace string using regex.

我正在使用正则表达式替换字符串。

How will i find the following string with decimal number.

我如何找到带有十进制数的以下字符串。

 Section 5.1.1
 Sections 5.1.2
 Sections 5.1.3 and 5.1.4
 Sec. 5.3.1

Can any one assist me?

任何人都可以帮助我吗?

Thanks in advance.

提前致谢。

Updated:

 (Sec(?:s\.|s|tions|tion|.)?)( |\s)?((\w+)?([.-]|(–)|(—))?(\w+))(\s+)?((\w+)?([.-]|(–)|(—))?(\w+))(\s+(to|and|through)\s(\w+)([.-]|(–)|(—))(\w+))?

1 个解决方案

#1


2  

How about

/(^|\s)(Sec\.|Sections?) +[0-9]+(\.[0-9]+)*( +and +[0-9]+(\.[0-9]+)*)*(?=$|\s)/

It will match substrings that

它会匹配那些子串

  • begin with whitespace or are at the beginning of the searched string
  • 以空格开头或位于搜索字符串的开头

  • are followed by whitespace or are at the end of the searched string
  • 后跟空格或位于搜索字符串的末尾

  • start with Sec., Section or Sections
  • 从Sec。,Section或Sections开始

  • followed by one or more spaces
  • 然后是一个或多个空格

  • followed by a section number
  • 然后是节号

  • optionally followed by one or more and [section number], where and is surrounded by one or more spaces.
  • 可选地后跟一个或多个和[section number],其中和被一个或多个空格包围。

Section numbers match if they

部分编号匹配,如果他们

  • start with one or more digits
  • 以一个或多个数字开头

  • optionally followed one or more times by a dot and again one or more digits.
  • 可选地,一个或多个点跟随一个或多个数字。

You can trim the match to get rid of eventual whitespace at the beginning of the match.

您可以修剪匹配以在匹配开始时删除最终的空格。

Update

/(^|[^\w])(Sec\.|Sections?) +[0-9]+(\.[0-9]+)*( +and +[0-9]+(\.[0-9]+)*)*(?=$|[^\w])/

This one matches substrings that

这个匹配子串

  • begin with a non-word character or are at the beginning of the searched string
  • 以非单词字符开头或位于搜索字符串的开头

  • are followed by a non-word character or are at the end of the searched string
  • 后跟一个非单词字符或位于搜索字符串的末尾

Rest same as above. Use an additional replace on the match to get rid of non-word character matches at the beginning: match.replace(/^[^\w]+/, '')

休息与上面相同。在匹配项上使用额外的替换来删除开头的非单词字符匹配:match.replace(/ ^ [^ \ w] + /,'')

#1


2  

How about

/(^|\s)(Sec\.|Sections?) +[0-9]+(\.[0-9]+)*( +and +[0-9]+(\.[0-9]+)*)*(?=$|\s)/

It will match substrings that

它会匹配那些子串

  • begin with whitespace or are at the beginning of the searched string
  • 以空格开头或位于搜索字符串的开头

  • are followed by whitespace or are at the end of the searched string
  • 后跟空格或位于搜索字符串的末尾

  • start with Sec., Section or Sections
  • 从Sec。,Section或Sections开始

  • followed by one or more spaces
  • 然后是一个或多个空格

  • followed by a section number
  • 然后是节号

  • optionally followed by one or more and [section number], where and is surrounded by one or more spaces.
  • 可选地后跟一个或多个和[section number],其中和被一个或多个空格包围。

Section numbers match if they

部分编号匹配,如果他们

  • start with one or more digits
  • 以一个或多个数字开头

  • optionally followed one or more times by a dot and again one or more digits.
  • 可选地,一个或多个点跟随一个或多个数字。

You can trim the match to get rid of eventual whitespace at the beginning of the match.

您可以修剪匹配以在匹配开始时删除最终的空格。

Update

/(^|[^\w])(Sec\.|Sections?) +[0-9]+(\.[0-9]+)*( +and +[0-9]+(\.[0-9]+)*)*(?=$|[^\w])/

This one matches substrings that

这个匹配子串

  • begin with a non-word character or are at the beginning of the searched string
  • 以非单词字符开头或位于搜索字符串的开头

  • are followed by a non-word character or are at the end of the searched string
  • 后跟一个非单词字符或位于搜索字符串的末尾

Rest same as above. Use an additional replace on the match to get rid of non-word character matches at the beginning: match.replace(/^[^\w]+/, '')

休息与上面相同。在匹配项上使用额外的替换来删除开头的非单词字符匹配:match.replace(/ ^ [^ \ w] + /,'')