Home > Web Front-end > JS Tutorial > body text

JS, replace uses regular expressions to replace all SQL parameters with data in the specified format

高洛峰
Release: 2018-05-24 10:31:53
Original
1827 people have browsed it

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

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

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

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

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

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

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!