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

The sample string: this!is.an?exampleI want to match: this is an example.I tried this:<script type

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745580390a4634221.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信