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, "&amp;")     //           .replace(/</g, "&lt;")     //           .replace(/>/g, "&gt;")     //           .replace(/"/g, "&quot;")     //           .replace(/'/g, "&#039;");      // uncaught typeerror: undefined not function   content.html(result); 

jsfiddle example

broken sanitize fiddle

var content = $('#content'),     html = content.html(),     result = html.match(/\w+\(.+?\)/g); var escapedres = result.replace(/&/g, "&amp;")     .replace(/</g, "&lt;")     .replace(/>/g, "&gt;")     .replace(/"/g, "&quot;")     .replace(/'/g, "&#039;")     .replace(/(/g, "&#40;")     .replace(/)/g, "&#41;")     .replace(/\*/g, "&#42;")     .replace(/$/g, "&#36;"); var result = escapedres.replace(result, '<pre>'+escapedres+'</pre>'); content.html(result); 

jsfiddle example

use regex:

/\w+\(.+?\)/g 

demo

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

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -