我有一个导航文本,我将选择*之后的最后一句话
示例;Home*Development*Mobil and Web Development
我将只选择* --> (Mobil和Web开发)之后的最后一句话
发布于 2021-10-26 06:15:15
如果要从文本中选择Mobil and Web Development,可以使用.split(/[*]+/).pop()。
演示
var n = "Home*Development*Mobil and Web Development".split(/[*]+/).pop();
console.log(n)
发布于 2021-10-26 06:22:03
您可以使用Javascript Regular express来实现这一点
let chain = "Home*Development*Mobil and Web Development";
let pattern = /.*\*(.*)$/
let matches = chain.match(pattern);
console.log(matches[1]);
发布于 2021-10-26 06:34:49
假设你想要做的是对最后的“句子”做一些样式,你可以在启动时运行一些JS来查找具有特定类的所有元素,分离出最后的部分并将其包装在span中,然后选择它:
const selectLasts = document.querySelectorAll('.selectLast');
selectLasts.forEach(selectLast => {
//note there are 'neater' ways of doing this but each step is deliberately spelled out here to show what is going on
const text = selectLast.innerHTML;
const arr = text.split('*');
const lastText = arr.pop();
selectLast.innerHTML = arr.join('*');
const lastEl = document.createElement('span');
lastEl.innerHTML = '*' + lastText;
selectLast.appendChild(lastEl);
});.selectLast span {
background-color: yellow;
}<div class="selectLast">Home*Development*Mobil and Web Development</div>
https://stackoverflow.com/questions/69718170
复制相似问题