如何在每 N 个字符后将一个字符串附加到另一个字符串?
How to append a string to another string after every N char?
我正在尝试创建一个程序,在写入字符串时每隔一个字母后添加“gav”。
var string1 = "word"
预期输出:
wogavrdgav
您可以为此使用模数运算符 -
var string1 = "word";
function addGav(str){
var newStr = '';
var strArr = str.split('');
strArr.forEach(function(letter, index){
index % 2 == 1
? newStr += letter + 'gav'
: newStr += letter
})
return newStr;
}
console.log(addGav(string1)); // gives wogavrdgav
console.log(addGav('gavgrif')) //gives gagavvggavrigavf....
正则表达式
在这里,我们可以在.
中添加一个量词(匹配除换行符之外的所有字符)并设计一个具有一个捕获组(</code>)的表达式:</p>
<pre><code>(.{2})
Demo
JavaScript 演示
const regex = /(.{2})/gm;
const str = `AAAAAA`;
const subst = `bbb`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
正则表达式电路
您还可以在 jex.im:
中可视化您的表情
如果您希望将新行视为字符,则可以使用此表达式:
([\s\S]{2})
RegEx Demo
JavaScript 演示
const regex = /([\s\S]{2})/gm;
const str = `ABCDEF
GHIJK
LMNOP
QRSTU
VWXYZ
`;
const subst = `bbb`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
试试这个:
const string1 = 'word'
console.log('Input:', string1)
const newStr = string1.replace(/(?<=(^(.{2})+))/g, 'gav')
console.log('Output:', newStr)
.{2}
: 2个任意字符
(.{2})+
:匹配2 4 6 8任意字符
^(.{2})+
: 匹配 2 4 6 8 开始的任何字符,如果没有 ^
,这个正则表达式将从任何位置开始匹配
?<=(regex_group)
:匹配regex_group
之后的内容
g
: 匹配全部
这种方法是从字符串的开头找到 2、4、6 等字符,但不匹配该组,因此它将匹配 2、4、6 等字符之前的 ''
并替换'gav'
示例word
:
匹配wo
,word
并忽略它,匹配之前的东西(''
)并用方法replace
[=26替换为'gav'
=]
我正在尝试创建一个程序,在写入字符串时每隔一个字母后添加“gav”。
var string1 = "word"
预期输出:
wogavrdgav
您可以为此使用模数运算符 -
var string1 = "word";
function addGav(str){
var newStr = '';
var strArr = str.split('');
strArr.forEach(function(letter, index){
index % 2 == 1
? newStr += letter + 'gav'
: newStr += letter
})
return newStr;
}
console.log(addGav(string1)); // gives wogavrdgav
console.log(addGav('gavgrif')) //gives gagavvggavrigavf....
正则表达式
在这里,我们可以在.
中添加一个量词(匹配除换行符之外的所有字符)并设计一个具有一个捕获组(</code>)的表达式:</p>
<pre><code>(.{2})
Demo
JavaScript 演示
const regex = /(.{2})/gm;
const str = `AAAAAA`;
const subst = `bbb`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
正则表达式电路
您还可以在 jex.im:
中可视化您的表情如果您希望将新行视为字符,则可以使用此表达式:
([\s\S]{2})
RegEx Demo
JavaScript 演示
const regex = /([\s\S]{2})/gm;
const str = `ABCDEF
GHIJK
LMNOP
QRSTU
VWXYZ
`;
const subst = `bbb`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
试试这个:
const string1 = 'word'
console.log('Input:', string1)
const newStr = string1.replace(/(?<=(^(.{2})+))/g, 'gav')
console.log('Output:', newStr)
.{2}
: 2个任意字符(.{2})+
:匹配2 4 6 8任意字符^(.{2})+
: 匹配 2 4 6 8 开始的任何字符,如果没有^
,这个正则表达式将从任何位置开始匹配?<=(regex_group)
:匹配regex_group
之后的内容
g
: 匹配全部
这种方法是从字符串的开头找到 2、4、6 等字符,但不匹配该组,因此它将匹配 2、4、6 等字符之前的 ''
并替换'gav'
示例word
:
匹配wo
,word
并忽略它,匹配之前的东西(''
)并用方法replace
[=26替换为'gav'
=]