Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: None  |  size: 0.53 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Regular Expression in JS
  2. var string = 'Doyletown, PA';
  3. var parts = string.split(',');
  4. if (parts.length > 0) {
  5.     var result = parts[0];
  6.     alert(result); // alerts Doyletown
  7. }
  8.        
  9. var str = 'Doyletown, PA';
  10. var newstr=str.substring(0,str.indexOf(',')) || str;
  11.        
  12. var str = "Doyletown, PA"
  13. var matches = str.match(/^([^,]+)/);
  14. alert(matches[1]);
  15.        
  16. //Gets all the words/sentences in a comma separated list and trims these words/sentences to get rid of outer spaces and other whitespace.
  17. var matches = str.match(/[^,s]+[^,]*[^,s]+/g);