R.Wieser wrote:
> Scott Sauyet wrote:,
>> Or possibly a few such examples?
> Of what ? I've given several examples (of the basic problem and how your
> approach does not quite work), but those are not the ones you seem to need.
I have some examples below. I'm *hoping* that they capture at least a
good chunk of your requirements, but it's not clear to me. If there are
gaps, could you supply such an example showing what you expect and how
this differs from that?
>> I'm now getting intrigued, but I still don't really understand the
>> problem.
> Ask yourself : does your approach solve the "this is a clbuttic mistake"
> problem ?
>
> 'Cause *that* is what I am after - just not for regular words as recognised
> by a RegExp (see "\w", "\W", "\b")
I don't think that anything can comfortably reverse the clbuttic
mistake. If you were to try, you'd probably end up with a "bellyasson"
in your navel. What I think you're doing is trying to find a way to
reverse the manual censoring of, say, `g**d***` which is too unlikely to
be intended text, and restore `goddamn`.
I'm trying for something simpler, still using regex. But obviously if
you can't do this with regex, you can still fall back on a custom state
machine implementation. That would be a shame.
No, but there is are different ways to answer the questions, "How do I
solve this real world problem?" and "How can I best use regex to solve
this problem?"
I don't know whether I should be trying to help you find a way to better
use regex to solve the problem, or looking more broadly at unrelated
techniques. That's why I asked.
> Also, do notice that I provided the correct solution in my first post.
> Only later I found out that the "look behind assertion" method itself isn't
> recognised by my FF v52 browsers JS. :-\
Right, you won't be able to use look-behind in FF before version 78. [1]
But as the current version is 101, and as version 52 was first released
more than five years ago, perhaps an update is in order?
Finally, here's another attempt, using a combination of a negative
look-behind before the target and a negative look-ahead after it. Does
this capture what you're trying to do?
```
const unclean = (w, r) => (s) =>
s .replace (new RegExp (
'\\b(?<!\\*)' + w .replace (/\*/g, '\\*') + '(?![\w*])',
'gi'
), r);
unclean ("g**d***", "goddamn") (""... a g**d*** word ...)
//=> "... a goddamn word ..."
unclean ("g**d***", "goddamn") (""... a d*** word ...)
//=> "... a d*** word ..."
unclean ("d***", "damn") (""... a g**d*** word ...)
//=> "... a g**d*** word ..."
unclean ("d***", "damn") (""... a d*** word ...)
//=> "... a damn word ..."
unclean ("d***", "damn") (""d*** this, d*** that, d*** everything!)
//=> "damn this, damn that, damn everything!"
unclean ("d***", "damn") (""Frankly, my dear, I don't give a d***!)
//=> "Frankly, my dear, I don't give a damn!"
unclean ("d***", "damn") (""Would I knew the villain! / I would lam-d*** him)
//=> "Would I knew the villain! / I would lam-damn him"
unclean ("d***", "damn") (""Teacher, what does 'landd***e' mean?)
//=> "Teacher, what does 'landd***e' mean?"
```
-- Scott
[1]: <
https://caniuse.com/js-regexp-lookbehind>