0

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
asked Jul 11, 2012 at 6:57
1
  • Could you clarify what exactly you mean by "array"? This looks (like @Confusion noted) more like a multiline string. Commented Jul 11, 2012 at 7:08

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

He seems to use a single multiline string as an array, so you might need an m modifier in addition to the g.
@Confusion: No, why? My regex doesn't contain any ^s or $s whose meaning would be affected by the /m modifier.
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.