Javascript 正则表达式:在word前面替换

Javascript 正则表达式:在word前面替换,javascript,regex,Javascript,Regex,我有这个字符串: const string = ` * @test * pm.test("Response time is less than 200ms", function() { * pm.expect(pm.response.responseTime).to.be.below(500); * }); * pm.test("Successful POST request", function() { * pm.expect(pm.response.code).to.be.oneOf

我有这个字符串:

const string = `
* @test
* pm.test("Response time is less than 200ms", function() {
*   pm.expect(pm.response.responseTime).to.be.below(500);
* });
* pm.test("Successful POST request", function() {
*   pm.expect(pm.response.code).to.be.oneOf([200, 201, 202]);
* });
`;
我想对它做一些更改,比如在每个
pm前面添加。期望
an
\n\t
和在每个
pm前面添加。测试
an
\n

const cleaned = string
    .replace(/\n/g, "")
    .replace(/\s */g, ' ')
    .replace(/\*/g, "")               
    .replace(/@[a-z]+/g, "")        
    .replace(/{(pm.expect)/g,'\n\t') // the problem is here
    .replace(/(pm.test)/g,'\n') // the problem is here
我希望最后有这样的东西:

pm.test("Response time is less than 200ms", function() {
  pm.expect(pm.response.responseTime).to.be.below(500);
});

pm.test("Successful POST request", function() {
  pm.expect(pm.response.code).to.be.oneOf([200, 201, 202]);
});
做:

您将得到:

"pm.test(\"Response time is less than 200ms\", function() { 
    pm.expect(pm.response.responseTime).to.be.below(500); }); 
pm.test(\"Successful POST request\", function() { 
    pm.expect(pm.response.code).to.be.oneOf([200, 201, 202]); });"
做:

您将得到:

"pm.test(\"Response time is less than 200ms\", function() { 
    pm.expect(pm.response.responseTime).to.be.below(500); }); 
pm.test(\"Successful POST request\", function() { 
    pm.expect(pm.response.code).to.be.oneOf([200, 201, 202]); });"
const字符串=`
*@测试
*pm.测试(“响应时间小于200ms”,功能(){
*pm.expect(pm.response.responseTime).to.be.低于(500);
* });
*pm.test(“成功的POST请求”,函数(){
*pm.expect(pm.response.code).to.be.of([200201202]);
* });
`;
常量=字符串
.替换(/\*\s+/g')
.替换(/@\w+/g'')
.替换(/(pm\.test)/g,“\r\n$1”)
.替换(/(pm\.expect)/g,“\t$1”)
.trim();
//显示结果。
document.getElementsByTagName('textarea')[0].value=cleanned
const字符串=`
*@测试
*pm.测试(“响应时间小于200ms”,功能(){
*pm.expect(pm.response.responseTime).to.be.低于(500);
* });
*pm.test(“成功的POST请求”,函数(){
*pm.expect(pm.response.code).to.be.of([200201202]);
* });
`;
常量=字符串
.替换(/\*\s+/g')
.替换(/@\w+/g'')
.替换(/(pm\.test)/g,“\r\n$1”)
.替换(/(pm\.expect)/g,“\t$1”)
.trim();
//显示结果。
document.getElementsByTagName('textarea')[0].value=cleanned

您可以使用replace及其回调来捕获组

^(\*\s*)(?:(pm.test)|(pm.expect)
   |          |           |__________   (Group 3, g3)       
   |          |______________________   (Group 2, g3)
   |_________________________________   (Group 1, g1)
const字符串=`
*@测试
*pm.测试(“响应时间小于200ms”,功能(){
*pm.expect(pm.response.responseTime).to.be.低于(500);
* });
*pm.test(“成功的POST请求”,函数(){
*pm.expect(pm.response.code).to.be.of([200201202]);
* });
`;
让op=string.replace(/^(\*\s*)(?:(pm.test)|(pm.expect))/gm,(match,g1,g2,g3)=>{
如果(g2){
返回g1+'\n\t'+g2
}否则{
返回g1+'\n'+g3
}
})

console.log(op)
您可以使用replace及其回调来捕获组

^(\*\s*)(?:(pm.test)|(pm.expect)
   |          |           |__________   (Group 3, g3)       
   |          |______________________   (Group 2, g3)
   |_________________________________   (Group 1, g1)
const字符串=`
*@测试
*pm.测试(“响应时间小于200ms”,功能(){
*pm.expect(pm.response.responseTime).to.be.低于(500);
* });
*pm.test(“成功的POST请求”,函数(){
*pm.expect(pm.response.code).to.be.of([200201202]);
* });
`;
让op=string.replace(/^(\*\s*)(?:(pm.test)|(pm.expect))/gm,(match,g1,g2,g3)=>{
如果(g2){
返回g1+'\n\t'+g2
}否则{
返回g1+'\n'+g3
}
})
console.log(op)
TL;博士 您可以使用lookaheads来处理匹配前的“插入”文本,并且不考虑空格

function clean(){
常量字符串=document.getElementById(“输入”).value;
常量=字符串
.替换(/\n/g,“”)
.替换(/\s*/g'')
.替换(/\*/g,“”)
.替换(/@[a-z]+//g,”)
.replace(/({)\s*(?=pm.expect)/g,“$1\n\t”)
.替换(/\s*(?=pm.test)/g,“\n”)
.replace(/(;)\s*(?=}\);)/g,$1\n');
document.getElementById(“结果”).textContent=已清理;
}

*@测试
*pm.测试(“响应时间小于200ms”,功能(){
*pm.expect(pm.response.responseTime).to.be.低于(500);
* });
*pm.test(“成功的POST请求”,函数(){
*pm.expect(pm.response.code).to.be.of([200201202]);
* });
清洁的
TL;博士 您可以使用lookaheads来处理匹配前的“插入”文本,并且不考虑空格

function clean(){
常量字符串=document.getElementById(“输入”).value;
常量=字符串
.替换(/\n/g,“”)
.替换(/\s*/g'')
.替换(/\*/g,“”)
.替换(/@[a-z]+//g,”)
.replace(/({)\s*(?=pm.expect)/g,“$1\n\t”)
.替换(/\s*(?=pm.test)/g,“\n”)
.replace(/(;)\s*(?=}\);)/g,$1\n');
document.getElementById(“结果”).textContent=已清理;
}

*@测试
*pm.测试(“响应时间小于200ms”,功能(){
*pm.expect(pm.response.responseTime).to.be.低于(500);
* });
*pm.test(“成功的POST请求”,函数(){
*pm.expect(pm.response.code).to.be.of([200201202]);
* });
清洁的
类似于
替换(/^\*/gm',)
?类似于
替换(/^\*/gm',)