Maison  >  Article  >  interface Web  >  JS、replace利用正则表达式替换SQL所有参数为指定格式的数据

JS、replace利用正则表达式替换SQL所有参数为指定格式的数据

高洛峰
高洛峰original
2018-05-24 10:31:531786parcourir

SQL参数格式 例如:select * from tb where nd=:nd and yd=:yd 
想一次性把所有SQL语句中参数(带冒号)全部换成数据, 
开始 
选定用正则表达式。 
原先写这样 

strsql.replace(/(:\w+)/g,(“$1”).substring(1));

"$1" 总是本解析成字符串,而不是匹配的值 
换成 

strsql.replace(/(:\w+)/g,$1);

又不能给出匹配值,$1 要想得到匹配值必须要带双引号。 

后突发奇想加了个方法 
把$1 当成参数传递,如下 

var strsql = strsql.replace(/(:\w+)/g, function ($1) { var b = $1; return $("#" + b.substring(1)).val(); });

更多JS、replace利用正则表达式替换SQL所有参数为指定格式的数据相关文章请关注PHP中文网!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn