如何用字符串末尾的空格替换<br/>? 和 如何使用其他数组的索引对列表进行排序



需要一个 JavaScript 正则表达式来删除字符串末尾的一个中断标记。

例:

输入:Sample br/ string that needs to remove br/ at the end br/ br/

输出:Sample br/ string that needs to remove br/ at the end br/

需要使用其他数组的索引对列表进行排序。

前任:
MyList[0] {name: abc, value:10, id : 2}
MyList[1] {name: ghf, value:123, id : 1}
MyList[1] {name: ghjs, value:12, id : 4}
MyList[1] {name: lhjsk, value:13, id : 5}

这里的 id 值存储在下面的数组中。我需要根据以下数组顺序进行排序。

数组[4, 1, 2, 5]

输出:
MyList[1] {name: ghjs, value:12, id : 4}
MyList[1] {name: ghf, value:123, id : 1}
MyList[0] {name: abc, value:10, id :2}
MyList[1] {name: lhjsk, value:13, id : 5}

您可以使用正则表达式生成器轻松测试正则表达式。在此处检查此正则表达式:regexr.com/4bv9f

let string = "Sample br/ string that needs to remove br/ at the end br/ br/",
regex = /br/$/,
result = string.replace(regex, '');
console.log('From:',string);
console.log('To:',result);

这不是

正则表达式,但您可以使用str.slice(0, -n)删除最后一个换行符,其中 n 是要从字符串中删除的字符数(在删除 br/ 的情况下为 3

(。

要删除字符串末尾的最后一个br/,您可以使用javascript的thr slice方法。以下是解决方案:

 var str = 'Sample br/ string that needs to remove br/ at the end br/ br/'
 var str1 = str.slice(0, str.length -3);

输出将如您所期望的那样:

需要删除 br/末尾的 br/的示例字符串 br/

最新更新