-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS基础测试38期 #45
Comments
var reg1 = /fill="([^"]*)"/g
var str1 = str.replace(reg1, function(m){
if(m === 'fill="none"'){
return 'fill="none"'
}
return ''
}) var base64Str = btoa(encodeURI(str1)) var reg3 = /[%#}{<>]/g
var str3 = str1.replace(reg3, function(m){
return encodeURI(m)
}) |
1. var str1 = str.replace(/fill="(?!none")[^"]+"/g,""); 2. var str2 = window.btoa(str1); 3. var str3 = str1.replace(/[%#{}<>]/g, encodeURI); |
第一题 str = str.replace(/fill\s*=\s*\"\s*(?!none)[^"]*\s*\"/g,"");
console.log(str); 第二题 console.log(window.btoa(str)); 第三题 console.log(str.replace(/["%#{}<>]/g,encodeURI)); |
第 1 题:// 方法1:利用 RegExp 的零宽断言
str = str.replace(/fill="(?!none)[^"]+"/gi, '');
// 方法2:先用 RegExp 通用判断,然后 replace 函数进一步判断
str = str.replace(/fill="([^"]+)"/gi, function($0, $1) {
return $1.toLowerCase() === 'none' ? $0 : '';
}); 第 2 题:
window.btoa(str) 第 3 题:
var reg_encodeChars = new RegExp('["%#{}<>]', 'g');
str = str.replace(reg_encodeChars, encodeURIComponent); |
第一题去除只有 fill 和 fill 不为 none 的属性 const noFillStr = str.replace(/fill\s*\=\s*"(.*?)"/g, (str, $1) => $1 !== 'none' ? '' : str).replace(/\s+(fill)\s+/,' ')
console.log(noFillStr) 第二题 const base64Str = btoa(noFillStr)
console.log(base64Str) 第三题 const encodeStr = noFillStr.replace(/(\"|\%|\#|\{|\}|\<|\>)/g, s => encodeURIComponent(s))
// 或者
const encodeStr = noFillStr.replace(/["%#{}<>"]/g, s => encodeURIComponent(s)) |
//第一题 |
let divElement = document.createElement('div')
divElement.innerHTML = str
divElement.childNodes[0].childNodes.forEach(child => {
console.log(child)
let fill = child.getAttribute('fill')
if (fill) {
child.removeAttribute('fill')
}
}) let base64Str = btoa(divElement.innerHTML) divElement.innerHTML.replace(/["%#{}<>]/g, encodeURI) |
var str = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2 2a1 1 0 0 1 1-1h13a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V2z" fill="#0067E6"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 6a1 1 0 0 1 1-1h9l5 5v12a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V6z" fill="#FEAEA5"/><path fill-rule="evenodd" clip-rule="evenodd" d="M17 5l5 5h-4a1 1 0 0 1-1-1V5z" fill="#0067E6"/></svg>';
// 第一题
let t1 = str.replace(/\sfill="none"/g, '')
// 第二题
let t2 = btoa(t1)
// 第三题
let t3 = t1.replace(/["%#{}<>]/g, encodeURIComponent) |
var str = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2 2a1 1 0 0 1 1-1h13a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V2z" fill="#0067E6"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 6a1 1 0 0 1 1-1h9l5 5v12a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V6z" fill="#FEAEA5"/><path fill-rule="evenodd" clip-rule="evenodd" d="M17 5l5 5h-4a1 1 0 0 1-1-1V5z" fill="#0067E6"/></svg>';
// 第一题
str = str.replace(/fill="[^(none)]*"/g, '');
// 第二题
var base64 = btoa(str);
// 第三题
var encodeStr = str.replace(/["%#{}<>]/g, s1 => encodeURI(s1)); |
// 1
var str1 = str.replace(/fill="(?!none)[^"]+"/g, '')
// 2
var str2 = window.btoa(str1)
// 3
不是很会 |
var str = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2 2a1 1 0 0 1 1-1h13a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V2z" fill="#0067E6"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 6a1 1 0 0 1 1-1h9l5 5v12a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V6z" fill="#FEAEA5"/><path fill-rule="evenodd" clip-rule="evenodd" d="M17 5l5 5h-4a1 1 0 0 1-1-1V5z" fill="#0067E6"/></svg>';
str1 = str.replace(/fill="([^"]+)"/g, function(kv, v){
return v === "none" ? kv: "";
});
console.log(str1);
str2 = btoa(str1);
console.log(btoa(str));
str3 = str1.replace(/[%#{}<>]/g, encodeURI);
console.log(str3); |
var str = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2 2a1 1 0 0 1 1-1h13a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V2z" fill="#0067E6"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 6a1 1 0 0 1 1-1h9l5 5v12a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V6z" fill="#FEAEA5"/><path fill-rule="evenodd" clip-rule="evenodd" d="M17 5l5 5h-4a1 1 0 0 1-1-1V5z" fill="#0067E6"/></svg>';
var firstAnswerRes = str.replace(/fill="[^(none)]+"/g, '')
// 1.
console.log(firstAnswerRes)
// 2.
console.log(btoa(firstAnswerRes))
// 3.
function urlEncodeChar(data) {
var data = firstAnswerRes.replace(/(")|(%)|(#)|(\{)|(\})|(<)|(>)/g, function (match, index, input) {
if (!match) {
return match
}
return encodeURI(match)
})
return data
}
console.log(urlEncodeChar(firstAnswerRes)) |
// 1.
str = str.replace(/[\s]+fill=(?!"none")[^\s\/\>]+/g, '')
//2.
window.btoa(str)
// 3.
str.replace(/["%#{}<>]/g, encodeURI) |
// 第一题
str.replace(/fill="(?!none")[^"]+"/g, "");
// 第二题
window.btoa(str);
// 第三题
str.replace(/["%#{}<>]/g, encodeURI); |
str = str.replace(new RegExp(' fill[ ]?=[ ]?(?:(?:\"(.*?)\")|(?:\'(.*?)\'))','ig'), function(str, group) {
return group !== 'none' ? '' : str;
}); window.btoa(str); str = str.replace(new RegExp('["%#{}<>]','g'), encodeURIComponent); |
//第一题
var str1 = str.replace(/fill="([^"]+)"/gi,function (match,p) {
return p === "none" ? match : '';
});
//第二题
var str2 = window.btoa(str1); //第三题
var str3 = str1.replace(/[,%#{}<>]/g,encodeURI); |
1.
let str1 = str.replace(/fill="([^"]+)"/g, function (match, $1) {
return $1 === 'none' ? match : ""
})
2.
let encodeData = window.btoa(str)
3.
let encodeStr = str.replace(/["%{}<>]/g, encodeURIComponent) |
var str = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2 2a1 1 0 0 1 1-1h13a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V2z" fill="#0067E6"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 6a1 1 0 0 1 1-1h9l5 5v12a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V6z" fill="#FEAEA5"/><path fill-rule="evenodd" clip-rule="evenodd" d="M17 5l5 5h-4a1 1 0 0 1-1-1V5z" fill="#0067E6"/></svg>';
// 1.
var transSvgStr = function(str) {
var parentDoc = document.createElement('div')
parentDoc.innerHTML = str
var list = parentDoc.querySelectorAll('[fill]')
list.forEach(e => {
if (e.getAttribute('fill') !== 'none') {
e.removeAttribute('fill')
}
})
return parentDoc.innerHTML
}
var newStr = transSvgStr(str)
console.log(newStr)
//2.
var base64Str = btoa(newStr)
console.log(base64Str)
//3.
var encodeStr = newStr.replace(/["%#{}<>]/g, encodeURI)
console.log(encodeStr) |
var str = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2 2a1 1 0 0 1 1-1h13a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V2z" fill="#0067E6"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 6a1 1 0 0 1 1-1h9l5 5v12a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V6z" fill="#FEAEA5"/><path fill-rule="evenodd" clip-rule="evenodd" d="M17 5l5 5h-4a1 1 0 0 1-1-1V5z" fill="#0067E6"/></svg>';
//zxx: 如果fill="null"也应该被替换
str=str.replace(/ fill="[^n"]+"/g ,'');
btoa(str);
str.replace(/["%#{}<>]/g, encodeURI) |
// 1
// 如果只是根据题目给出的 str 可以使用简单的正则匹配
var str1 = str.replace(/fill=\"#\w+\"/g, '');
// 或者是使用零宽断言
var str1_1 = str.replace(/fill=\"(?!none)#?\w+\"/g, '')
// 2
var str2 = window.btoa(str1)
// 3
var str3 = str1.replace(/["%#{}<>]/g, encodeURI) |
// 1.
var str1 = str.replace(/fill="(?!none")[^"]+"/g, "");
// 2.
var str2 = window.btoa(str1);
// 3.
var str3 = str.replace(/["%#{}<>]/g, encodeURI); |
不好意思,张老师我这个没有时间做了。打个卡。 |
var newStr = str.replace('/fill="[^(none)]*?"/g', "")
console.log(newStr);
console.log(window.btoa(newStr));
console.log(newStr.replace(/["%#{}<>]/g, encodeURI)); |
// 第一题
const str1 = str.replace(/fill="(?!none)[^"]+"/g, '');
// 第二题
const str2 = window.btoa(str1);
// 第三题
const str3 = str1.replace(/[,%${}<>]/g, encodeURI) |
// 1
var removeFill = (string) => {
let temp = string.split('/>').join(' ').split(' ')
for (let i = 0; i < temp.length; i++) {
const strs = temp[i];
if (strs.startsWith('fill=') && strs.includes('#')) {
temp[i] = '/>'
}
}
return temp.join(' ')
}
let s1 = removeFill(str)
// 2
let s2 = window.btoa(s1)
// 3
let s3 = encodeURIComponent(s1) |
第一问 let newStr = str.replace(/fill="(?!none")[^"]+"/g, "") 第二问 btoa(newStr) 第三问 str.replace(/["%#{}<>]/g, encodeURI) |
var str = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2 2a1 1 0 0 1 1-1h13a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V2z" fill="#0067E6"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 6a1 1 0 0 1 1-1h9l5 5v12a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V6z" fill="#FEAEA5"/><path fill-rule="evenodd" clip-rule="evenodd" d="M17 5l5 5h-4a1 1 0 0 1-1-1V5z" fill="#0067E6"/></svg>';
第一题
var newStr = str.replace(/fill="none"/g, "");
第二题
var bit = window.btoa(newStr);
第三题
var codeStr = str.replace(/["%#{}<>]/g,encodeURI);
|
本期要点:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
关于SVG字符串处理,比较基本:
每小题2积分。
题目中的字符串如下:
答题前不要看别人回答,答题后可以,但不能修改自己回答
大家提交回答的时候,注意缩进距离,起始位置从左边缘开始;另外,github自带代码高亮,所以请使用下面示意的格式(1积分)。
其它:
The text was updated successfully, but these errors were encountered: