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-Z40円]\b. but it will be example(dot)om rather then example(dot)com.
Jon Adams
25.3k18 gold badges85 silver badges122 bronze badges
-
Could you clarify what exactly you mean by "array"? This looks (like @Confusion noted) more like a multiline string.Tim Pietzcker– Tim Pietzcker2012年07月11日 07:08:56 +00:00Commented Jul 11, 2012 at 7:08
1 Answer 1
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".
answered Jul 11, 2012 at 6:59
Tim Pietzcker
338k59 gold badges521 silver badges572 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Confusion
He seems to use a single multiline string as an array, so you might need an
m modifier in addition to the g.Tim Pietzcker
@Confusion: No, why? My regex doesn't contain any
^s or $s whose meaning would be affected by the /m modifier.Confusion
No, you are right, it doesn't matter. I thought
/m also influenced the meaning of \b, but a quick perusal of the pcre docs show that isn't the case.lang-js