正则表达式
模板 tpl
var reg = /^1\d{10}$/ <% // 正则表达式中出现分隔符? tjs正常解析😄, ejs则会报错 // 可以copy这段代码到 ejs2 运行一下 https://ionicabizau.github.io/ejs-playground/ var re = /^[\u4e00-\u9fa5]+<%分隔符%>$/ var url = 'https://search.jd.com/search?keyword=x&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&ev=exbrand_Apple%5E&uc=0#J_searchWrap' function getParam (name, url) { var reg = new RegExp('[?&]' + name + '=([^?]*)'); var tmp = (url || location.href).match(reg); return tmp && tmp[1] || ''; } var atypeof = 1234 %> 1.<%-re.test('你<%分隔符%>')%> 2.<%-/2+3/+2/3/4%> 3.<%-(/2+3/+2)/3/4%> 4.<%-/2+3/.test('2223')%> 5.<%/2"3+'"/gi%> 6.<%-void /ab"c/%> 7.<%-typeof /ab"%>c/%> 8.<%-atypeof / 2%> 9.<%typeof /ab"%>c/%> 10.<%-typeof( /ab"%>c/)%> 11.<%-'test' in /ab"c/%> 12.<%-getParam('enc', url)%> 13.<%-delete url%> 14.<%typeof url === 'string'?%>It's string<%:%>undefined<%;%> 15.<%/ab"c/.test('ab"cd') ?%>true<%:%>false<%;%> 16.<%/\d+"/.test('12"') && %>'1<%-/abc/.test('abc')%>2'<% || %>'34'
使用
var render = tjs(tpl); render();
输出