Regex Guid

Regex Guid

by litespeedtdf

JavaScript

//isGuid("abc");
var tests = [
    "not a guid",         
    "3C8021B0-423D-475D-BECF-63ED5ED3456dd",
    "3c8021b0-423d-475d-becf-63ed5ed34563" 
    ];
		
//console.log(tests[0]);
//console.log(tests[1]);
//console.log(tests[2]);


for (var i = 0; i < tests.length; i++) {
	isGuid3(tests[i]);
   //console.log(tests[i] + ' ' + isGuid(tests[i]));
   // document.write(tests[i] + " : " + isGuid2(tests[i]) + "<br/>");
}

function isGuid(str){

	const regex = /(([0-9A-F]{8})-([0-9A-F]{4})-([0-9A-F]{4})-([0-9A-F]{4})-([0-9A-F]{12}))/gi;
	//const str = `1DB0DEC0-1AF0-4261-9B8B-3AA47687DBD4`;
	
	
	//var m = regex.exec(str);
	//console.log(m);
		
	var a = str.search(regex);
	console.log(a);
	/*
	let m;

	while ((m = regex.exec(str)) !== null) {
			// This is necessary to avoid infinite loops with zero-width matches
			if (m.index === regex.lastIndex) {
					regex.lastIndex++;
			}

			// The result can be accessed through the `m`-variable.
			m.forEach((match, groupIndex) => {
					console.log(`Found match, group ${groupIndex}: ${match}`);
			});
	}
*/

}

function isGuid2(str){
	const regex = /^[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}?$/gi;
	let m;
	while ((m = regex.exec(str)) !== null) {
			// This is necessary to avoid infinite loops with zero-width matches
			if (m.index === regex.lastIndex) {
					regex.lastIndex++;
			}
			console.log(m);
			// The result can be accessed through the `m`-variable.
			m.forEach((match, groupIndex) => {
					console.log(`Found match, group ${groupIndex}: ${match}`);
			});
	}
}


function isGuid3(str){
	const regex = /^[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}?$/gi;
	let m;
	m = regex.exec(str);
	if(m){
		console.log(regex.lastIndex);
		console.log(m[0]);
	}

	/*
	while ((m = regex.exec(str)) !== null) {
			// This is necessary to avoid infinite loops with zero-width matches
			if (m.index === regex.lastIndex) {
					regex.lastIndex++;
			}
			console.log(m);
			// The result can be accessed through the...