views:

136

answers:

5

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>
+4  A: 

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.

SLaks
I want to use regex , and for every punctuations char in english.
ronik
The . inside your character range should be escaped with \. Now this regex splits on every character. Also, even when escaped, this only works on !, . and ?, whereas ronik wanted that it works on all punctuation.
Tatu Ulmanen
@Tatu: You are wrong. `.` does not need to be escaped inside a character class. Please try it before downvoting.
SLaks
@Ronik: You can put whatever punctuation characters you want inside the brackets.
SLaks
I need some general brake like \W
ronik
I don't think there is a character class that matches only punctuation.
SLaks
A: 

If you just want to match:

(\w|\.|!|\?)+
John Dibling
this doesn't work
ronik
I use http://myregextester.com/index.php for testing
ronik
+3  A: 

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|_/);
Tatu Ulmanen
Is it possilbe to do it with match or exec?
ronik
@ronik, can you explain why you need to do a match? What is your goal? I assumed you just want words that are separated by punctuation replaced with words separated by spaces.
Tatu Ulmanen
@ronik: `match` returns a string's substring, so the answer is: no it's not possible to get match() to return a "this is an example" on your input string.
Nickolay
A: 

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.

Richard
A: 

/^\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
slebetman