javascript 简单的正则表达式匹配多个带有空格/多个空格或没有空格的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13614727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:17:33  来源:igfitidea点击:

simple regex to matching multiple word with spaces/multiple space or no spaces

javascriptjavaregexstring

提问by paul

I am trying to match all words with single or multiple spaces. my expression

我试图用单个或多个空格匹配所有单词。我的表情

(\w+\s*)* is not working

(\w+\s*)* 不起作用

edit 1: Let say i have a sentence in this form

编辑 1:假设我有一个这种形式的句子

[[do "hi i am bob"]]
[[do "hi   i am Bob"]]

now I have to replace this with 
cool("hi i am bob") or 
cool("hi    i am Bob")

I do not care about replacing multiple spaces with single .

我不在乎用 single 替换多个空格。

I can achieve this for a single word like

我可以用一个词来实现这个

\[\[do\"(\w+)\"\]\] and replacing regex cool\(\"$1\")but this does not look like an effective solution and does not match multiple words .... I apologies for incomplete question

\[\[do\"(\w+)\"\]\] and replacing regex cool\(\"$1\")但这看起来不是一个有效的解决方案,并且不匹配多个单词......我为不完整的问题道歉

any help will be aprecciated

任何帮助都会受到赞赏

回答by FrankieTheKneeMan

Find this Regular Expression:

找到这个正则表达式:

/\[\[do\s+("[\w\s]+")\s*\]\]/

And do the following replacement:

并进行以下替换:

'cool()'

The only special thing that's being done here is using character classes to our advantage with

这里做的唯一特别的事情是使用字符类来为我们提供优势

[\w\s]+

Matches one or more word or space characters (a-z, A-Z, 0-9, _, and whitespace). That';; eat up your internal stuff no problem.

匹配一个或多个单词或空格字符(az、AZ、0-9、_ 和空格)。那';; 吃掉你的内部东西没问题。

'[[do "hi   i am Bob"]]'.replace(/\[\[do\s+("[\w\s]+")\s*\]\]/, 'cool()')

Spits out

吐出来

cool("hi   i am Bob")

Though - if you want to add punctuation (which you probably will), you should do it like this:

虽然 - 如果你想添加标点符号(你可能会这样做),你应该这样做:

/\[\[do\s+("[^"]+")\s*\]\]/

Which will match any character that's not a double quote, preserving your substring. There are more complicated ones to allow you to deal with escaped quotation marks, but I think that's outside the scope of this question.

这将匹配任何不是双引号的字符,保留您的子字符串。有更复杂的方法可以让您处理转义引号,但我认为这超出了本问题的范围。

回答by ?mega

To match "all words with single or multiple spaces", you cannot use \s*, as it will match even no spaces.

要匹配“带有单个或多个空格的所有单词”,您不能使用\s*,因为它甚至会匹配没有空格。

On the other hand, it looks like you want to match even "hi", which is one word with no spaces.

另一方面,看起来您甚至想匹配“hi”,这是一个没有空格的单词。

You probably want to match one or more words separated by spaces. If so, use regex pattern

您可能想要匹配一个或多个由空格分隔的单词。如果是这样,请使用正则表达式模式

(\w+(?:$|\s+))+

or

或者

\w+(\s+\w+)*

回答by VisioN

I'm not sure, but maybe this is what you're trying to get:

我不确定,但也许这就是你想要得到的:

"Hi I am bob".match(/\b\w+\b/g);  // ["Hi", "I", "am", "bob"]

回答by kzh

What your regex is doing is:

您的正则表达式正在做的是:

/([a-zA-Z0-9_]{1,}[ \r\v\n\t\f]{0,}){0,}/

That is, find the first match of one or more of A through Z bother lower and upper along with digits and underscore, then followed by zero or more space characters which are:

也就是说,找到 A 到 Z 中的一个或多个的第一个匹配项以及数字和下划线,然后是零个或多个空格字符,它们是:

  • A space character
  • A carriage return character
  • A vertical tab character
  • A new line character
  • A tab character
  • A form feed character
  • 一个空格字符
  • 一个回车符
  • 垂直制表符
  • 换行符
  • 一个制表符
  • 换页符

Then followed by zero or more of A through Z bother lower and upper along with digits and underscore.

然后是零个或多个 A 到 Z 以及数字和下划线。

\smatches more than just simple spaces, you can put in a literal space, and it will work.

\s匹配的不仅仅是简单的空格,您可以放入文字空格,它会起作用。

I believe you want:

我相信你想要:

/(\w+ +\w+)/g

Which allmatches of one or more of A through Z bother lower and upper along with digits and underscore, followed by oneor more spaces, then followed by one or more of A through Z bother lower and upper along with digits and underscore.

其中一个或多个 A 到 Z 的所有匹配项与数字和下划线一起上下划线,然后是一个或多个空格,然后是 A 到 Z 中的一项或多项以及数字和下划线。

This will match all word-charactersseparated by spaces.

这将匹配所有由空格分隔的单词字符

If you just want to find all clusters of word characters, without punctuation or spaces, then, you would use:

如果您只想查找没有标点符号或空格的所有单词字符簇,则可以使用:

/(\w+)/g

Which will find all word-charactersthat are grouped together.

这将找到组合在一起的所有单词字符

回答by Bruno

Simple. Match all groups of characters that are not white spaces

简单的。匹配所有非空格字符组

var str = "Hi I   am   Bob";

var matches = str.match(/[^ ]+/g); // => ["Hi", "I", "am", "Bob"]

回答by Christophe

var regex=/\w+\s+/g;

Live demo: http://jsfiddle.net/GngWn/

现场演示:http: //jsfiddle.net/GngWn/

[Update] I was just answering the question, but based on the comments this is more likely what you're looking for:

[更新] 我只是在回答这个问题,但根据评论,这更有可能是您要查找的内容:

var regex=/\b\w+\b/g;

\b are word boundaries.

\b 是单词边界。

Demo: http://jsfiddle.net/GngWn/2/

演示:http: //jsfiddle.net/GngWn/2/

[Update2] Your edit makes it a completely different question:

[Update2] 您的编辑使它成为一个完全不同的问题:

string.replace(/\[\[do "([\s\S]+)"\]\]/,'cool("")');

Demo: http://jsfiddle.net/GngWn/3/

演示:http: //jsfiddle.net/GngWn/3/

回答by Christophe

Use regex pattern \w+(\s+\w+)*as follows:

使用正则表达式模式\w+(\s+\w+)*如下:

m = s.match(/\w+(\s+\w+)*/g);