我有这样一个字符串:
- - This is the place where the people like to join think-tanks and have fun.显然,我可以使用以下两种方法将所有连字符替换为空格:
my_string.replace(/-/g, ' ')
my_string.split('-').join(' ')我只需要用空格替换智库中的连字符。其他连字符可以单独使用。
I如何仅针对字符串中字母之间的连字符
发布于 2020-01-07 14:58:05
只需使用\w捕获字母,然后用空格替换破折号“
str.replace(/(\w)\-(\w)/gi, '$1 $2')元字符是[0-9a-zA-Z_]的缩写,即包括数字、大小写字母和下划线。
const str = '- - This is the place where the people like to join think-tanks and have fun.';
console.log(str.replace(/(\w)\-(\w)/gi, '$1 $2'));
https://stackoverflow.com/questions/59630864
复制相似问题