
Untitled
By: a guest on
Jun 14th, 2012 | syntax:
None | size: 0.53 KB | hits: 20 | expires: Never
Regular Expression in JS
var string = 'Doyletown, PA';
var parts = string.split(',');
if (parts.length > 0) {
var result = parts[0];
alert(result); // alerts Doyletown
}
var str = 'Doyletown, PA';
var newstr=str.substring(0,str.indexOf(',')) || str;
var str = "Doyletown, PA"
var matches = str.match(/^([^,]+)/);
alert(matches[1]);
//Gets all the words/sentences in a comma separated list and trims these words/sentences to get rid of outer spaces and other whitespace.
var matches = str.match(/[^,s]+[^,]*[^,s]+/g);