Regex Favs
These are some of my favorite Regular Expressions to use. Code samples are Javascript. Usually I will use combinations of these to get the results I'm looking for. Right now I only have a few, plan on adding more as I come across em & write em.
Tools
- Regular-Expressions.info
Regex Tutorial, Examples and Reference, Regexp Patterns - RegExr
Learn, Build, & Test RegEx
Character Removal/Manipulation
Non-ASCII Characters
javascripts = s.replace(/[^\x{21}-\x{7E}\s\t\n\r]/g,'');
Control Characters
javascripts = s.replace(/[:cntrl:]/g,'');
javascriptvar s = 'aaaa \t\r\nbbbb cccc \n\t ddd\n\n\n';
s = s.replace(/[\s+]/g,' ');
javascrirptvar s = ' something ';
s = s.replace(/^[\ \t]+|[\ \t]+$/g,'');
javascriptvar s = 'https://a.b/?regex=stripquerystring';
s = s.replace(/^(.+)(\?.*)$/gm,'$1');
YouTube
javascriptvar s = 'https://youtube.com/watch?v=7woVTuN8k3c',
vid = s.replace(/(.*?)(^|\/|v=)([a-z0-9_-]{11})(.*)?/gi,'$3');
javascriptvar s = 'https://youtube.com/watch?v=7woVTuN8k3c',
valid = /^(.+)?(https?\:\/\/)(www\.)?(youtu\.?be\/?)(\.com\/)?(watch|embed)?.+?([a-z0-9_-]{11})(.+)?$/gim.test(s);