Skip to content

Instantly share code, notes, and snippets.

@Telematica
Last active December 19, 2016 22:47
Show Gist options
  • Save Telematica/6a55b3c1002933b88f527cb45f0ac2f3 to your computer and use it in GitHub Desktop.
Save Telematica/6a55b3c1002933b88f527cb45f0ac2f3 to your computer and use it in GitHub Desktop.
JS Regex Sample Collection
;(function(){
console.log(
/^([a-zA-Z]{3,4})-(\d{6})-((\D|\d){3})?$/.test('OIHG-551205-2L7'), //Check RFC Format
/[\r\n]+/.test('ABCD\n \t EFGHI\n \t ') //Select Line,
/(?=.*?\bbundle\b)(?=.*?\bstandalone\b)^.*$/i.test('standalone bundle'), //
/(?=.*?\bbundle\b)(?=.*?\bstandalone\b)^.*$/i.test('standalone? bundle'), //
/(?=.*?\bbundle\b)(?=.*?\bstandalone\b)^.*$/i.test('standalone_ bundle'), //
'/^[a-f0-9]{32}$/'.test('5d41402abc4b2a76b9719d911017c592'), // MD5 check
/cmb[\d\w-_]+/.test('cmbCountry'), //
' _ A + B _ '.replace(/^\s+|\s+$/g, ""), // Emulates trim()
',A,B,C,D,E,F,G,'.replace( /^,|,$/g, "" ), //Emulates trim() using a specific char to trim
'abcde_12345'.match( /[0-9]+$/ ), // Matches a number at the end of the line
'-1.233 assaas -1.455 343434'.match(/(-+)?\d+(\.\d{1,2})?/g) //Matches a (un)signed decimal number and trucates on 2 dec.
'1-2-3-4-5-6-7-8-9-0---0----989-8-8-A-Z------X'.match(/([^\-]*)$/)[0] //Matches last occurrence: http://stackoverflow.com/questions/8374742/regex-last-occurrence
);
console.log(
(function()
{
var val = "1.02345";
( ( val.match( /(\d+)?(\.\d{1,2})?/ ) !== null )
? ( ( val.match( /(\d+)?(\.\d{1,2})?/ )[1] !== undefined || ( val.match( /(\d+)?(\.\d{1,2})?/ )[2] !== undefined ) )
? val.match( /(\d+)?(\.\d{1,2})?/ )[ 0 ].toString()
: null )
: null )
})();
);
console.log(
( regex = /(\d+)?(\.\d{1,2})?/ ).toString(), " \n",
"" .match( regex ), " \n",
"." .match( regex ), " \n",
".1" .match( regex ), " \n",
".12" .match( regex ), " \n",
".123" .match( regex ), " \n",
"1" .match( regex ), " \n",
"1." .match( regex ), " \n",
"1.2" .match( regex ), " \n",
"1.23" .match( regex ), " \n",
"1.234" .match( regex ), " \n"
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment