用正则表达式将多个字符替换为一个字符 [英] Replace multiple characters by one character with regex

查看:318
本文介绍了用正则表达式将多个字符替换为一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串:

var str = '#this #is____#a test###__'

我想用(#)替换所有字符(#,_),所以例外的输出是:

I want to replace all the character (#,_) by (#) , so the excepted output is :

'#this #is#a test#'

注意:我不知道字符串中(#)或(_)有多少顺序

Note : I did not knew How much sequence of (#) or (_) in the string

我尝试什么:

我尝试写:

var str = '#this #is__ __#a test###__'
str = str.replace(/[#_]/g,'#')
alert(str)

但是输出是:

#this #is## ###a test#####

我在线尝试

我尝试将(*)用于序列,但没有用:

I try to use the (*) for sequence But did not work :

var str = '#this #is__ __#a test###__'
str = str.replace(/[#_]*/g,'#')
alert(str)

那么我如何获得我的例外输出?

so How I can get my excepted output ?

推荐答案

编写良好的RegEx可以轻松解决您的问题.引用莫希特的答案有一个起点:

A well written RegEx can handle your problem rather easily. Quoting Mohit's answer to have a startpoint:

var str = '#this #is__ __#a test###__';
var formattedStr = str.replace(/[#_,]+/g, '#');
console.log( formattedStr );

第2行:
将替换方法在str上的结果放入formattedStr中.
替换工作如何?第一个参数是字符串或RegEx.
注意:Javascript中的RegExps是RegExp类型的对象,而不是字符串.所以写

Line 2:
Put in formattedStr the result of the replace method on str.
How does replace work? The first parameter is a string or a RegEx.
Note: RegExps in Javascripts are Objects of type RegExp, not strings. So writing

/yourRegex/

New RegExp('/yourRegex/')

是等效的语法.现在让我们讨论这个特定的RegEx本身.
前导斜杠和尾部斜杠用于包围样式,末尾的g表示全局"-在第一场比赛后不要停止.
方括号描述了可以提供以匹配模式的一组字符,而+号表示该组中的1个或多个".
基本上,###将匹配,但# # ### # _#也会匹配,因为_和#属于同一集合.
首选行为将通过使用(#| _)+
给出这意味着#或_,一旦找到一个,就继续期待更多或所选模式".
因此___和####都会匹配,但是__ ##是2个不同的匹配组(前者是__,后者是##).
另一个问题是不知道如何用_或#替换找到的模式.
幸运的是,使用括号可以使我们使用一种称为捕获组的东西.基本上,您可以存储在临时变量中找到的任何模式,这些模式可用于替换模式.
调用它们很容易,将$附加到匹配的(模式)的位置.
例如,/(foo)textnotgetting(bar)captured(baz)/将以这种方式填充捕获组变量":

is equivalent syntax. Now let's discuss this particular RegEx itself.
The leading and trailing slashes are used to surround the pattern, and the g at the end means "globally" - do not stop after the first match.
The square parentheses describe a set of characters who can be supplied to match the pattern, while the + sign means "1 or more of this group".
Basically, ### will match, but also # or #####_# will, because _ and # belong to the same set.
A preferred behavior would be given by using (#|_)+
This means "# or _, then, once found one, keep looking forward for more or the chosen pattern".
So ___ would match, as well as #### would, but __## would be 2 distinct match groups (the former being __, the latter ##).
Another problem is not knowing wheter to replace the pattern found with a _ or a #.
Luckily, using parentheses allows us to use a thing called capturing groups. You basically can store any pattern you found in temporary variabiles, that can be used in the replace pattern.
Invoking them is easy, propend $ to the position of the matched (pattern).
/(foo)textnotgetting(bar)captured(baz)/ for example would fill the capturing groups "variables" this way:

$1 = foo
$2 = bar
$3 = baz

在我们的例子中,我们只想用第一个出现的字符替换1+个字符,并且括号中不包含+号!
所以我们可以简单地

In our case, we want to replace 1+ characters with the first occurrence only, and the + sign is not included in the parentheses!
So we can simply

str.replace("/(#|_)+/g", "$1");

为了使其正常工作.

祝你有美好的一天!

In order to make it work.

Have a nice day!

这篇关于用正则表达式将多个字符替换为一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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