简体   繁体   English

替换字符串中间的点

[英]Replace dot in middle of a string

Let's say we have an array (new line separated) like: 假设我们有一个数组(用新行分隔):

hello
example.com
test.
something.
 aspacefront
test
test.us

to be an array 成为一个数组

hello
example(dot)com.
test.
something.
 aspacefront
test
test(dot)us

Can this be done with a regex? 可以用正则表达式完成吗?

What I've up to loop the array then replace to (dot) with this regex \\b[^a-zA-Z\\40]\\b. 我要循环的数组,然后用此正则表达式\\b[^a-zA-Z\\40]\\b.替换为(点) \\b[^a-zA-Z\\40]\\b. but it will be example(dot)om rather then example(dot)com. 但是它将是example(dot)om,而不是example(dot)com。

The simplest solution: 最简单的解决方案:

result = subject.replace(/\b\.\b/g, "(com)");

This means "Replace a dot only if it is preceded and followed by an alphanumeric character". 这意味着“仅当在圆点之前和之后带有字母数字字符时才替换圆点”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM