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
Bowser3mo 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
Eighth3mo ago
Thanks, but I can't access the GPT chat
Bowser
Bowser3mo ago
Eighth
Eighth3mo ago
Many thanks
ἔρως
ἔρως3mo 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
Eighth3mo 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
ἔρως
ἔρως3mo 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
Eighth3mo 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
ἔρως
ἔρως3mo ago
then can you show an example? because that sounds like something that shouldnt be a long regex
Eighth
Eighth3mo ago
Here's an example – I lik@e to eat ap@ple@@s
ἔρως
ἔρως3mo ago
and you need to remove the @?
Eighth
Eighth3mo ago
Its and any 1 character in front of it, it turns out I need to remove them in pairs
ἔρως
ἔρως3mo ago
so, what would be the final result?
Eighth
Eighth3mo ago
For example, a@pp@@les -> a@p@les - > a@les - > les
ἔρως
ἔρως3mo ago
oh, it has to be recursive??? yickes!
Eighth
Eighth3mo 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
ἔρως
ἔρως3mo ago
sounds like /.@/g is enough and then you loop while /.@/ matches
Eighth
Eighth3mo 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
ἔρως
ἔρως3mo ago
ah, then [^@] instead of . should do it
Eighth
Eighth3mo ago
I'll give it a try now
ἔρως
ἔρως3mo 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
Eighth3mo ago
That's the only thing I know about replace
ἔρως
ἔρως3mo 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
Eighth3mo ago
I haven't had to work with them until now
ἔρως
ἔρως3mo ago
they are easier than you think
Eighth
Eighth3mo ago
Soon I'll be learning php
ἔρως
ἔρως3mo 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
Eighth3mo ago
Sounds promising
ἔρως
ἔρως3mo ago
it is a lot easier, but js had regex literals
Eighth
Eighth3mo ago
To me, it looks like a spell
ἔρως
ἔρως3mo ago
you will get used to it, don't worry
Eighth
Eighth3mo ago
This option seems to have worked. I hope I understand, which is why I wanted to find a detailed description of these symbols
ἔρως
ἔρως3mo ago
dont forget to replace the . with [^@]
Eighth
Eighth3mo ago
Everything worked, only there was 1 symbol left, where there was a double one, probably I need to throw it into the loop
ἔρως
ἔρως3mo ago
you do as i said here
Eighth
Eighth3mo ago
I'm confused about something, should I use inside the math condition?
ἔρως
ἔρως3mo ago
basically, this:
while(/[^@]@/.test(value)) {
value = value.replace(/[^@]@/g, '');
}
while(/[^@]@/.test(value)) {
value = value.replace(/[^@]@/g, '');
}
Eighth
Eighth3mo ago
Thank you, sorry for being dumb
ἔρως
ἔρως3mo ago
it's not about being dumb or smart its practice and lack of
Eighth
Eighth3mo ago
I agree, I just haven't had to deal with this before, thank you so much again for your help
ἔρως
ἔρως3mo ago
you're welcome just out of curiosity, which regex did you wrote so far, to solve this problem?
Eighth
Eighth3mo ago
Like you said, I just didn't understand what matches were at first
ἔρως
ἔρως3mo 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
Eighth3mo ago
I wrote- string.replace(/[^#]#/g,'')
ἔρως
ἔρως3mo 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
Eighth3mo ago
I am given a symbol that needs to be tracked, I think these points are taken into account there
ἔρως
ἔρως3mo ago
does the symbol change?
Eighth
Eighth3mo ago
Yes
ἔρως
ἔρως3mo ago
fucj fuck and is it always just a single symbol?
Eighth
Eighth3mo ago
One, at first it could be @, then #, or something else
ἔρως
ἔρως3mo ago
can there be 2 different symbols in the search? for example: "a@pp#@le"
Eighth
Eighth3mo ago
At the same time, no
ἔρως
ἔρως3mo ago
thats a bit easier then