如何选择最后一个*后面的句子



我有一个导航文本,我将选择*后的最后一句话

例子;Home*Development*Mobil and Web Development

我将只选择*——>之后的最后一句话;(移动和Web开发)

如果您想从文本中选择Mobil and Web Development,则可以使用.split(/[*]+/).pop()

var n = "Home*Development*Mobil and Web Development".split(/[*]+/).pop();
console.log(n)

可以使用Javascript正则表达式

let chain = "Home*Development*Mobil and Web Development";
let pattern = /.**(.*)$/
let matches = chain.match(pattern);
console.log(matches[1]);

假设你想做的是对最后的'句子'进行一些样式化,你可以在启动时运行一些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>

您可以像这样在div中插入子元素,然后使用CSS进行选择。

div > span {
color:red;
}
<div>
Home "" Development "" <span>Mobil and Web Development</span>
</div>

相关内容