正则表达式来更改字符串中的某些单词

时间:2022-11-28 21:36:34

I am trying to get my head around regular expressions. I thought the following would work, but unfortunately it isn't.

我试图了解正则表达式。我认为以下方法可行,但遗憾的是并非如此。

What I want to do:

我想做的事:

Given a string str (see below) I want to replace any word that is not shade or gravel with the word gravel.

给定一个字符串str(见下文),我想用单词gravel替换任何不是阴影或砾石的单词。

str = "gravel shade water grass people water shade";
output = str.replace(/([^gravel|^shade])/g,' gravel ');  

output should equal gravel shade gravel gravel gravel gravel shade. What I have is close enough but a bit off.

输出应该等于碎石砾石砾石碎石砾石色调。我所拥有的是足够近但有点偏。

2 个解决方案

#1


2  

Your ([^gravel|^shade]) matches and captures into Group 1 any one single character that is not g, r, a, v, e, l, |, ^, and replace all of them with gravel.

你的([^ gravel | ^ shade])匹配并捕获任何一个不是g,r,a,v,e,l,|,^的单个字符,并用砾石替换所有这些字符。

You can use

您可以使用

/\b(?!(?:shade|gravel)\b)\w+\b/g

See the regex demo

请参阅正则表达式演示

Pattern description:

  • \b - leading word boundary
  • \ b - 领先的单词边界

  • (?!(?:shade|gravel)\b) - a negative lookahead that will exclude matching whole words shade and gravel
  • (?!(?:shade | gravel)\ b) - 一个消极的前瞻,将排除匹配的整个单词阴影和砾石

  • \w+ - 1+ word characters (belonging to the [A-Za-z0-9_] set)
  • \ w + - 1+个单词字符(属于[A-Za-z0-9_]集)

  • \b - trailing word boundary.
  • \ b - 尾随字边界。

var str = 'gravel shade water grass people water shade';
var result = str.replace(/\b(?!(?:shade|gravel)\b)\w+\b/g, 'gravel');
document.body.innerHTML = result;

#2


0  

Here is a non-regex solution for your problem using split/join and array.map:

以下是使用split / join和array.map解决问题的非正则表达式解决方案:

var str = "gravel shade water grass people water shade";
var repl = str.split(' ').map(function (w) {
           return (w!='gravel' && w!= 'shade')?'gravel':w; }).join(' ')

document.writeln("<pre>" + repl + "</pre>")
//=> "gravel shade gravel gravel gravel gravel shade"

#1


2  

Your ([^gravel|^shade]) matches and captures into Group 1 any one single character that is not g, r, a, v, e, l, |, ^, and replace all of them with gravel.

你的([^ gravel | ^ shade])匹配并捕获任何一个不是g,r,a,v,e,l,|,^的单个字符,并用砾石替换所有这些字符。

You can use

您可以使用

/\b(?!(?:shade|gravel)\b)\w+\b/g

See the regex demo

请参阅正则表达式演示

Pattern description:

  • \b - leading word boundary
  • \ b - 领先的单词边界

  • (?!(?:shade|gravel)\b) - a negative lookahead that will exclude matching whole words shade and gravel
  • (?!(?:shade | gravel)\ b) - 一个消极的前瞻,将排除匹配的整个单词阴影和砾石

  • \w+ - 1+ word characters (belonging to the [A-Za-z0-9_] set)
  • \ w + - 1+个单词字符(属于[A-Za-z0-9_]集)

  • \b - trailing word boundary.
  • \ b - 尾随字边界。

var str = 'gravel shade water grass people water shade';
var result = str.replace(/\b(?!(?:shade|gravel)\b)\w+\b/g, 'gravel');
document.body.innerHTML = result;

#2


0  

Here is a non-regex solution for your problem using split/join and array.map:

以下是使用split / join和array.map解决问题的非正则表达式解决方案:

var str = "gravel shade water grass people water shade";
var repl = str.split(' ').map(function (w) {
           return (w!='gravel' && w!= 'shade')?'gravel':w; }).join(' ')

document.writeln("<pre>" + repl + "</pre>")
//=> "gravel shade gravel gravel gravel gravel shade"