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

javascript - Match words separated by punctuation characters using regex - Stack Overflow

programmeradmin2浏览0评论

The sample string:

this!is.an?example

I want to match: this is an example.

I tried this:

<script type="text/javascript">
   var string="this!is.an?example"; 
   var pattern=/^\W/g;
   alert(string.match(pattern));
</script>

The sample string:

this!is.an?example

I want to match: this is an example.

I tried this:

<script type="text/javascript">
   var string="this!is.an?example"; 
   var pattern=/^\W/g;
   alert(string.match(pattern));
</script>
Share Improve this question edited Jan 1, 2010 at 21:06 Svante 51.6k11 gold badges83 silver badges125 bronze badges asked Jan 1, 2010 at 17:08 ronikronik 3892 gold badges5 silver badges14 bronze badges 1
  • What exactly are you trying to do? – SLaks Commented Jan 1, 2010 at 17:22
Add a ment  | 

5 Answers 5

Reset to default 5

Try this:

var words = "this!is.an?example".split(/[!.?,;:'"-]/);

This will create an string array containing each word.
If you want to turn it into a single string with the words separated by spaces, you can call words.join(" ").

EDIT: You could also split on \W (str.split(/\W/)), but that may match more characters than you want.

I can't understand why you want to explicitly match, but if your goal is to strip all punctuation, this would work:

var words = "this!is.an?example".split(/\W/);
words = words.join(' ');

\W will match any character except letters, digits and underscore.

If you want to split also on underscores, use this:

var words = "this!is.an?example_with|underscore".split(/\W|_/);

If you just want to match:

(\w|\.|!|\?)+

If you want to replace all punctuation with a whitespace, you could do this:

var str = str.replaceAll([^A-Za-z0-9]," ");

This replaces all non letters, numerals with a space.

/^\W/g means match a string where the first character is not a letter or number

and the string "this!is.an?example" obviously does not begin with a non-letter or non-number.

Remember that ^ means the whole string start with, not what you want to match start with. And also remember that capital \W is everything that is not matched by small \w. With that reminder what you probably want is:

var string="this!is.an?example"; 
var pattern=/(\w+)/g;                   // parens for capturing

alert(string.match(pattern).join(' ')); // if you don't join,
                                        // some browsers will simply
                                        // print "[object Object]" 
                                        // or something like it
发布评论

评论列表(0)

  1. 暂无评论