jquery - Regex match javascript/php function name in str -
i'm trying highlight code (and sanitize html) regex not matching function name , params. no @ regex, kinda boggles me begin with. when try use .replace()
on matched result sanitize html , add <pre>
brackets, gives me error uncaught typeerror: undefined not function
guessing because it's not returning basic string?
var content = $('#content'), html = content.html(), result = html.replace(/\s.*\(.*\)\s/gi, "<pre>$&</pre>"); // when trying add <pre> tags in last line of code // , use sanitize html.match() error // escapedres = result.replace(/&/g, "&") // .replace(/</g, "<") // .replace(/>/g, ">") // .replace(/"/g, """) // .replace(/'/g, "'"); // uncaught typeerror: undefined not function content.html(result);
broken sanitize fiddle
var content = $('#content'), html = content.html(), result = html.match(/\w+\(.+?\)/g); var escapedres = result.replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """) .replace(/'/g, "'") .replace(/(/g, "(") .replace(/)/g, ")") .replace(/\*/g, "*") .replace(/$/g, "$"); var result = escapedres.replace(result, '<pre>'+escapedres+'</pre>'); content.html(result);
use regex:
/\w+\(.+?\)/g
update:
on sanitizing part, need
result = html.match(/\w+\(.+?\)/g)[0];
as match() returns array.
also, you'll need escape (
, )
, $
backslash, have special meaning in regex.
Comments
Post a Comment