twiss 155 Veteran Poster

Hi all,

I'm trying to match all PHP reserved keywords in a string, but this string also contains a bunch of \4 characters between the letters.
So, for example, if I have the string 'f\4u\4n\4c\4t\4i\4o\4n@@@some other weird characters@@@' and the array ['abstract','and','array','as','break','case','catch','class','clone','const','continue','declare','default','do','else','elseif','enddeclare','endfor','endforeach','endif','endswitch','endwhile','extends','final','for','foreach','function','global','goto','if','implements','interface','instanceof','namespace','new','or','private','protected','public','static','switch','throw','try','use','var','while','xor'] and I want to make 'function' match '\4f\4u\4n\4c\4t\4i\4o\4n\4', but only if it's not preceded, nor followed by a letter.

I tried:

var php_keywords = function() {
	var phpkl = ['abstract','and','array','as','break','case','catch','class','clone','const','continue','declare','default','do','else','elseif','enddeclare','endfor','endforeach','endif','endswitch','endwhile','extends','final','for','foreach','function','global','goto','if','implements','interface','instanceof','namespace','new','or','private','protected','public','static','switch','throw','try','use','var','while','xor'], phpks = '';
	for(var i = 0; i < phpkl.length; i++) for(var j = 0; j < phpkl[i].length; j++) phpks += (j > 0 ? '\4' : i > 0 ? '|' : '') + phpkl[i][j];
	console.log((phpks).replace(/\4/g, '\\4'));
	return new RegExp(phpks, 'g');
}();

but this matches in 'a\4f\4u\4n\4c\4t\4i\4o\4n\4a', not in '@\4f\4u\4n\4c\4t\4i\4o\4n\4@'

phpks becomes a\4b\4s\4t\4r\4a\4c\4t|a\4n\4d|a\4r\4r\4a\4y|a\4s|b\4r\4e\4a\4k|c\4a\4s\4e|c\4a\4t\4c\4h|c\4l\4a\4s\4s|c\4l\4o\4n\4e|c\4o\4n\4s\4t|c\4o\4n\4t\4i\4n\4u\4e|d\4e\4c\4l\4a\4r\4e|d\4e\4f\4a\4u\4l\4t|d\4o|e\4l\4s\4e|e\4l\4s\4e\4i\4f|e\4n\4d\4d\4e\4c\4l\4a\4r\4e|e\4n\4d\4f\4o\4r|e\4n\4d\4f\4o\4r\4e\4a\4c\4h|e\4n\4d\4i\4f|e\4n\4d\4s\4w\4i\4t\4c\4h|e\4n\4d\4w\4h\4i\4l\4e|e\4x\4t\4e\4n\4d\4s|f\4i\4n\4a\4l|f\4o\4r|f\4o\4r\4e\4a\4c\4h|f\4u\4n\4c\4t\4i\4o\4n|g\4l\4o\4b\4a\4l|g\4o\4t\4o|i\4f|i\4m\4p\4l\4e\4m\4e\4n\4t\4s|i\4n\4t\4e\4r\4f\4a\4c\4e|i\4n\4s\4t\4a\4n\4c\4e\4o\4f|n\4a\4m\4e\4s\4p\4a\4c\4e|n\4e\4w|o\4r|p\4r\4i\4v\4a\4t\4e|p\4r\4o\4t\4e\4c\4t\4e\4d|p\4u\4b\4l\4i\4c|s\4t\4a\4t\4i\4c|s\4w\4i\4t\4c\4h|t\4h\4r\4o\4w|t\4r\4y|u\4s\4e|v\4a\4r|w\4h\4i\4l\4e|x\4o\4r .

Any suggestions? If using PHP or jQuery somehow helps, that's OK.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.