Replace

Hello. Can someone tell me where to find a detailed explanation of writing paternas to the replace() method? I need the most detailed explanation, because I need to write a large paterna
53 Replies
Bowser
Bowser8mo ago
https://regexr.com/, and ChatGPT is also really good for generating Regex
RegExr
RegExr: Learn, Build, & Test RegEx
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
Eighth
EighthOP8mo ago
Thanks, but I can't access the GPT chat
Bowser
Bowser8mo ago
Eighth
EighthOP8mo ago
Many thanks
ἔρως
ἔρως8mo ago
if you need to write a large pattern, you need to stop and think about it why it must be done in regex
Eighth
EighthOP8mo ago
My goal is to find a specific character and remove that character and any character before it and so on, all over the line
ἔρως
ἔρως8mo ago
that's easy you want to, basically, remove a word that contains a specific character? if that's not it, do you have a good example?
Eighth
EighthOP8mo ago
It's not a word, I have arbitrary strings, my task is to remove a certain character and a letter or any other character that will come before it
ἔρως
ἔρως8mo ago
then can you show an example? because that sounds like something that shouldnt be a long regex
Eighth
EighthOP8mo ago
Here's an example – I lik@e to eat ap@ple@@s
ἔρως
ἔρως8mo ago
and you need to remove the @?
Eighth
EighthOP8mo ago
Its and any 1 character in front of it, it turns out I need to remove them in pairs
ἔρως
ἔρως8mo ago
so, what would be the final result?
Eighth
EighthOP8mo ago
For example, a@pp@@les -> a@p@les - > a@les - > les
ἔρως
ἔρως8mo ago
oh, it has to be recursive??? yickes!
Eighth
EighthOP8mo ago
It doesn't matter, the main thing is that it is step-by-step, but recursion is not very suitable, because the line can be very long and the stack will just overflow
ἔρως
ἔρως8mo ago
sounds like /.@/g is enough and then you loop while /.@/ matches
Eighth
EighthOP8mo ago
There is no specific order, the main goal is to remove them in pairs in any order, but there is a condition that it does not remove its copy, but only pairs with other symbols
ἔρως
ἔρως8mo ago
ah, then [^@] instead of . should do it
Eighth
EighthOP8mo ago
I'll give it a try now
ἔρως
ἔρως8mo ago
that is a negative character class that matches anything that is not inside the brackets the . matches anything the @ matches the @ /g tells the engine to do a global search instead of stopping after the first match
Eighth
EighthOP8mo ago
That's the only thing I know about replace
ἔρως
ἔρως8mo ago
now you know a tiny bit more but this isnt specific to replace, but many regex engines but not all variants - E.g.: php doesnt have the g flag
Eighth
EighthOP8mo ago
I haven't had to work with them until now
ἔρως
ἔρως8mo ago
they are easier than you think
Eighth
EighthOP8mo ago
Soon I'll be learning php
ἔρως
ἔρως8mo ago
you will have a much better time with regexes in php php supports an x flag that lets you indent and comment stuff and you can use other separators, to make it easier for you to write the regex without a trillion slashes
Eighth
EighthOP8mo ago
Sounds promising
ἔρως
ἔρως8mo ago
it is a lot easier, but js had regex literals
Eighth
EighthOP8mo ago
To me, it looks like a spell
ἔρως
ἔρως8mo ago
you will get used to it, don't worry
Eighth
EighthOP8mo ago
This option seems to have worked. I hope I understand, which is why I wanted to find a detailed description of these symbols
ἔρως
ἔρως8mo ago
dont forget to replace the . with [^@]
Eighth
EighthOP8mo ago
Everything worked, only there was 1 symbol left, where there was a double one, probably I need to throw it into the loop
ἔρως
ἔρως8mo ago
you do as i said here
Eighth
EighthOP8mo ago
I'm confused about something, should I use inside the math condition?
ἔρως
ἔρως8mo ago
basically, this:
while(/[^@]@/.test(value)) {
value = value.replace(/[^@]@/g, '');
}
while(/[^@]@/.test(value)) {
value = value.replace(/[^@]@/g, '');
}
Eighth
EighthOP8mo ago
Thank you, sorry for being dumb
ἔρως
ἔρως8mo ago
it's not about being dumb or smart its practice and lack of
Eighth
EighthOP8mo ago
I agree, I just haven't had to deal with this before, thank you so much again for your help
ἔρως
ἔρως8mo ago
you're welcome just out of curiosity, which regex did you wrote so far, to solve this problem?
Eighth
EighthOP8mo ago
Like you said, I just didn't understand what matches were at first
ἔρως
ἔρως8mo ago
a "match" is something that fulfills the rules of the regular expression in case of /[^@]@/ it is very simple if there is an <@ or x@, it will fulfill all requirements of it and that is said to have matched, because there was a match that fulfilled the rules
Eighth
EighthOP8mo ago
I wrote- string.replace(/[^#]#/g,'')
ἔρως
ἔρως8mo ago
that works too you do have to be careful with which symbols you put where, by the way but there should be a reference of all symbols with special meaning in mdn
Eighth
EighthOP8mo ago
I am given a symbol that needs to be tracked, I think these points are taken into account there
ἔρως
ἔρως8mo ago
does the symbol change?
Eighth
EighthOP8mo ago
Yes
ἔρως
ἔρως8mo ago
fucj fuck and is it always just a single symbol?
Eighth
EighthOP8mo ago
One, at first it could be @, then #, or something else
ἔρως
ἔρως8mo ago
can there be 2 different symbols in the search? for example: "a@pp#@le"
Eighth
EighthOP8mo ago
At the same time, no
ἔρως
ἔρως8mo ago
thats a bit easier then
Want results from more Discord servers?
Add your server