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