我需要在匹配的'/'字符之间删除字符串的一部分。例如:
'/this is/ a sentence' => ' a sentence'
我使用正则表达式的经验很少,所以不确定如何去做或者我甚至需要它。
我知道我可以使用子字符串,但想知道如何使用正则表达式或者是否可以使用正则表达式。
目前我有
const string = '/this is/ a sentence';
const substring = string.substring(4);
这对于正则表达式方面很好但很好奇
只是使用\/[^\/]*\/
正则表达式并用空字符串替换它?
var s = '/this is/ a sentence'
console.log(s + ' --> ' + s.replace(/\/[^\/]*\//g, ''))
这个正则表达式\/[^\/]*\/
匹配一个/
然后任何字符除了/
零次或多次然后再次/
并用空字符串替换它。
你可以使用\/.*?\/
\/
- 匹配/
.*?
- 匹配除新行之外的任何内容(懒惰模式)let str = '/this is/ a sentence'
let op = str.replace(/\/.*?\//g,'')
console.log(op)
有了这个正则表达式:
var str = "'/this is/ a sentence' => ' a sentence'";
var re = str.replace(/(?!<\")\/[^\*]+\/(?!\")/g,'')
console.log("re: " + re);