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