Reputation: 5439
I have text like this:
....foo..bar....
foo and bar can be any text at all.
It could also be:
anypreamble....foo..bar....anypostamble
I'm trying to match just foo and just bar but I keep matching the entire thing.
Here's some code:
var s = "this....AAAAAAAAAA..BBBBCD....that";
console.log(s.replace(/\.{4}.+\.{2}(.+)\.{4}/g, 'X'));
I would expect the above to give me: thisAAAAAAAAAAXthat, instead it gives: thisXthat.
Can you help?
Here's a fiddle: https://jsfiddle.net/vfkzdg9y/1/
Upvotes: 0
Views: 48
Reputation: 9782
If you want to get the two strings (foo and bar) separated with X, you can use:
var s = "this....AAAAAAAAAA..BBBBCD....that";
console.log(s.replace(/[^\.]*\.{4}([^\.]+)\.{2}([^\.]+)\.{4}.*/g, '$1X$2'));
Yields
AAAAAAAAAAXBBBBCD
You could also use:
console.log(s.replace(/[^\.]*\.{4}(.+)\.{2}(.+)\.{4}.*/g, '$1X$2'));
Which yields
AAAAAAAAAAXBBBB.CD
for your second example.
Upvotes: 1
Reputation: 1
Try using RegExp
/(\.{2}\w+)(?=\.{4}\w+$)|\./g
to match two consecutive .
characters followed by alphanumeric characters , if followed by four .
characters followed by alphanumeric characters followed by end of string ; replace .
with empty string, else return "X"
for captured match
s.replace(/(\.{2}\w+)(?=\.{4}\w+$)|\./g, function(match) {
return match === "." ? "" : "X"
})
Upvotes: 0