最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - simple regex to matching multiple word with spacesmultiple space or no spaces - Stack Overflow

programmeradmin0浏览0评论

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

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

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

[[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 .

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 inplete question

any help will be aprecciated

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

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

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

[[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 .

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 inplete question

any help will be aprecciated

Share Improve this question edited May 23, 2018 at 15:22 Ωmega 43.7k35 gold badges142 silver badges211 bronze badges asked Nov 28, 2012 at 21:26 paulpaul 1,1249 gold badges27 silver badges45 bronze badges 5
  • 1 What are you really try to acplish? Are you trying to isolate each word? Find a sequence of characters not including punctuation? What exactly is the actual goal here? – jfriend00 Commented Nov 28, 2012 at 21:28
  • whatchu trying to get out of that there regex?> – thatidiotguy Commented Nov 28, 2012 at 21:28
  • @jfriend00 & thatidiotguy my apologies for inplete question .Please check I have updated the question – paul Commented Nov 28, 2012 at 21:48
  • What a rapid edit - now some "old" answers look like dumb ones. – Ωmega Commented Nov 28, 2012 at 22:49
  • 1 -1 agree with @Ωmega, with the update this thread is pretty much useless for future generations... – Christophe Commented Nov 28, 2012 at 23:15
Add a ment  | 

7 Answers 7

Reset to default 3

Find this Regular Expression:

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

And do the following replacement:

'cool($1)'

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.

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

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 plicated ones to allow you to deal with escaped quotation marks, but I think that's outside the scope of this question.

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

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

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

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

or

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

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"]

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

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

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"]

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 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.

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

I believe you want:

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

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

This will match all word-characters separated 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-characters that are grouped together.

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

Live demo: http://jsfiddle/GngWn/

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

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

\b are word boundaries.

Demo: http://jsfiddle/GngWn/2/

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

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

Demo: http://jsfiddle/GngWn/3/

发布评论

评论列表(0)

  1. 暂无评论