替换除第一个以外的所有

xBl*_*lue 6 javascript regex

是否可以替换除第一个之外的所有事件?所以123.45.67..89.0应该成为123.4567890.

编辑:我正在寻找一个正则表达式。我知道如何使用 concat 或使用索引来做到这一点。

Phi*_*ipp 8

您可以使用积极的回顾来实现这一点:

(?<=\..*)\.
Run Code Online (Sandbox Code Playgroud)

所以你的代码将是

"123.45.67..89.0".replace(/(?<=\..*)\./g, '');
Run Code Online (Sandbox Code Playgroud)

  • Safari 不支持此功能(撰写本文时为 v14.1) (2认同)

Mr.*_*r.7 6

使用JS:

var str = "123.45.67.89.0";

var firstOccuranceIndex = str.search(/\./) + 1; // Index of first occurance of (.)

var resultStr = str.substr(0, firstOccuranceIndex) + str.slice(firstOccuranceIndex).replace(/\./g, ''); // Splitting into two string and replacing all the dots (.'s) in the second string

console.log(resultStr);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)


小智 0

尝试这个

var a = '123.45.67.89.0';

a.split('.')[0].concat('.'+a.split('.')[1]+a.split('.')[2]+a.split('.')[3]+a.split('.')[4])
Run Code Online (Sandbox Code Playgroud)