当前位置:网站首页>关于正则的两道笔试面试题
关于正则的两道笔试面试题
2022-07-28 18:26:00 【程序媛_MISS_zhang_0110】
一、第一题
页面中存在id=hxContainer的DOM元素。
该DOM元素内会给出一段随机文本,可能包含一些链接,比如https://www.baidu.com,或者www.baidu.com?from=leadtay。
如果出现链接文本,请给该链接文本加上链接标签,用户点击后能直接在新窗口中打开该链接。
请用javascript完成 link函数,完成该功能
1、hxContainer只有纯文本内容,不包含其他dom元素
2、识别所有以http://、https://或者www.开始的链接
3、所有www.开头的链接,默认使用 http 协议
4、所有链接在新窗口打开
function link() {
}
1.主要代码
function link() {
var obj = document.getElementById('jsContainer')
var container = obj.innerHTML
// console.log(container)
var reg = /(http:\/\/|https:\/\/)?www.[-A-Za-z0-9+&@#/%?~=_|!:,.;]+/g
// ? 表示匹配零次或一次
// + 表示匹配 一次到多次 (至少有一次)
// var res = null
// while((res=reg.exec(container))!==null){
// console.log(res[0])
// }
// var res = reg.exec(container)
// console.log(res)
var result = container.replace(reg, function (text) {
console.log(text)
console.log('aaaa')
// ^ 表示匹配字符串的开始位置
if(/^www./.test(text)){
text = 'http://'+text
}
return '<a target="_blank" href='+text+'>'+text+'</a>';
})
obj.innerHTML = result
}
link()
2.扩展知识
2.1 查找一个字符串中所有出现的索引
var str = "I learned to play the Ukulele in Lebanon."
var regex = /le/gi, result, indices = [];
while ( (result = regex.exec(str)) ) {
//它通过指定的模式(pattern)搜索字符串,并返回已找到的文本
indices.push(result.index);
}
2.2 js识别字符串中的链接并使之可跳转
function translateHtml(msg){
var reg = /((http|https):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|])/g;
var textR = msg.replace(reg, "<a href='$1' target='_blank' style='color: #00a0e9;width: 360px;display: inline;'>$1</a>");
return textR;
}
var regexp = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\&|-)+)/g;
content = content.replace(regexp, function($url){
//replace() 方法返回模式被替换处修改后的字符串。
return "<a href='" + $url + "' target='_blank'>" + $url + "</a>";
});
console.log(content);
2.3 正则中replace中$
replace改变字符串颜色
<script language="javascript">
var str="迈阿密热火获得了2011-2012赛季NBA总冠军,我爱迈阿密!";
var newStr=str.replace(/(迈阿密)/g,"<font color=red>$1</font>");
document.write(newStr);
</script>
输入查找字符串
<script language="javascript">
//查找字符串,并将匹配的字符串变为红色
document.write("迈阿密热火获得了2011-2012赛季NBA总冠军,我爱迈阿密!");
var s=prompt("请输入要查找的字符","迈阿密");
var reg=new RegExp("("+s+")","g");
var str="迈阿密热火获得了2011-2012赛季NBA总冠军,我爱迈阿密!";
var newstr=str.replace(reg,"<font color=red>$1</font>"); //$1表示左边表达式中括号内的内容,即第一个子匹配,同理可得$2表示第二个子匹配
//可能大家都会对$1这个特殊字符表示什么意思不是很理解,其实$1表示的就是左边表达式中括号内的字符,即第一个子匹配,同理可得$2表示第二个子匹配。。什么是子匹配呢??通俗点讲,就是左边每一个括号是第一个字匹配,第二个括号是第二个子匹配。。
document.write(newstr);
</script>
二、第二题
css中经常有类似background-image这种通过-连接的字符,通过javascript 设置样式的时候需要将这种样式转换成backgroundImage驼峰格式,请创建函数实现此转换功能:
- 以-为分隔符,将第二个起的非空单词首字母转为大写
- 每个单词仅有首字母大写
- 若开头为-,则删除
- 非字母保持不变
示例
输入 输出
1 font-size fontSize
2 Font-NAME fontName
3 -webkit-border-image webkitBorderImage
4 table-style-1 tableStyle1
2.1 主要代码
function cssStyle2DomStyle(sName) {
let pname=sName.split('-')
for(let i=0,len=pname.length;i<len;i++){
if(pname[i]===''){
pname.splice(i,1)
}
}
// console.log(pname)
let Str=''
for(let index=0,len=pname.length;index<len;index++){
if(index==0){
Str += pname[index]
}else{
let Fletter=pname[index].slice(0,1).toUpperCase()
let Rest=pname[index].slice(1)
Str +=Fletter+Rest
}
}
return Str
}
let str='-webkit-border-image'
console.log(cssStyle2DomStyle(str))
function getDomStyle(sName) {
let newCss = []
let dataList = sName.split('-')
if (!dataList[0]) {
dataList.shift()
}
console.log(dataList)
dataList.forEach((item, index) => {
if (index == 0) {
newCss[index] = item.toLowerCase()
} else {
let newLetter = []
let newStr = ''
let secondItem = item.split('')
if (/^[a-zA-Z]*$/.test(secondItem[0])) {
// ^ 表示匹配字符串的开始位置
// * 表示匹配 零次到多次
let newLetter = []
secondItem.forEach((pitem, pindex) => {
if (pindex == 0) {
newLetter.push(pitem.toUpperCase())
} else {
newLetter.push(pitem.toLowerCase())
}
})
newStr = newLetter.join('')
} else {
newStr = item
}
newCss[index] = newStr
}
})
return newCss.join('')
}
console.log(
getDomStyle('font-size'),
getDomStyle('Font-NAME'),
getDomStyle('-webkit-border-image'),
getDomStyle('table-style-1')
)
链接: https://blog.csdn.net/yexudengzhidao/article/details/84968787
链接: https://www.it1352.com/1003715.html
链接: https://www.csdn.net/tags/MtTaMg4sOTQ4ODE2LWJsb2cO0O0O.html
链接: https://blog.csdn.net/iteye_2449/article/details/81767762?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-81767762-blog-7685836.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-81767762-blog-7685836.pc_relevant_default&utm_relevant_index=2
链接: https://blog.csdn.net/w4bobo/article/details/7685836
链接: https://blog.csdn.net/weixin_40119412/article/details/108145026
边栏推荐
- JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs
- Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
- Use of DDR3 (axi4) in Xilinx vivado (3) module packaging
- Use of DDR3 (axi4) in Xilinx vivado (1) create an IP core
- Use of DDR3 (axi4) in Xilinx vivado (5) board test
- 一碰撞就自燃,谁来关心电池安全?上汽通用有话说
- MySQL batch update data
- MySQL startup error 1607 unexpected process termination
- Linux Installation MySQL (pit filling version)
- Common instructions of vim software
猜你喜欢

One article makes you understand what typescript is

Linxu 【基本指令】
![Maximum exchange [greedy thought & monotonic stack implementation]](/img/ad/8f0914f23648f37e1d1ce69086fd2e.png)
Maximum exchange [greedy thought & monotonic stack implementation]

【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)

Solve the cookie splitting problem (DP)

Raspberry pie 4B parsing PWM

NEIL: Extracting Visual Knowledge from Web Data

Torch. NN. Linear() function

Anaconda creation environment

Shanghai Jiaotong University joined hands with Taobao to set up a media computing laboratory: promoting the development of key technologies such as video super score
随机推荐
9. Pointer of C language (1) what is pointer and how to define pointer variables
Linxu [basic instructions]
Raspberry pie 3b ffmpeg RTMP streaming
Speech controlled robot based on ROS (I): realization of basic functions
树行表达方式
WUST-CTF2021-re校赛wp
Raspberry Pie 3 connected to WiFi
Merge sort template
Power Bi 2021 calendar DAX code
Scene thread allocation in MMO real-time combat games
How to automatically store email attachments in SharePoint
CM4 development cross compilation tool chain production
Solve the problem that the nocturnal simulator cannot access the Internet after setting an agent
The privatized instant messaging platform protects the security of enterprise mobile business
[link url]
Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
Maximum exchange [greedy thought & monotonic stack implementation]
LeetCode_位运算_中等_260.只出现一次的数字 III
C language - control statement
Raspberry pie 4B deploy yolov5 Lite using ncnn