Useful regex:s

by yong

JavaScript

// Test a swedish zip code
document.write( /\d{5}|\d{3}\s\d{2}/.test('12345') );
document.write( /\d{5}|\d{3}\s\d{2}/.test('123 45') );

// Test email simple
document.write( /^\S+@\S+\.\S+$/.test('[email protected]') );

// Test numbers and spaces (phone number)
document.write( /^[0-9\s]+$/.test('070 123 456') );

// Replace urls with hyperlinks
var exp = /(\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$])/ig;
var text = 'A url http://www.dn.se in a text';
document.write( text.replace(exp, '<a href="$1" target="_blank">$1</a>') )

// Test url (WHY U NO WORK?)
document.write( /\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]/.test('http://www.dn.se') )