Just a few things I’d like to talk about in this one please?
First thing:
Correct me if I’m wrong but the replace method is actually doing 2 things…it’s searching for the wording (which needs the replacements) and then it replaces, so…in actual fact the regex has to pretty much do 2 things here, search for the original text…which should match, and then it must also match the new wording, am I wrong what i’m saying?
Secondly:
why are we using the “|”, I know its meaning but its supposed to end with either one or the other as correct…not BOTH??? This just doesn’t make sense? because if one is correct then the other isn’t? How is it possible to use a conditional logic to appease both
Your code so far
let hello = " Hello, World! ";
let wsRegex =/^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex, ""); // Change this line
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
No, I’m saying that the pattern matching doesn’t stop just because one of the sub patterns matches. The “g” global flag says to find as many possible matches as possible, in other words, don’t stop after the first match. That’s why it can match both white space at the beginning of the string and white space at the end of the string.
I can understand why “g” is there…but the"|"? I still don’t get why the “I” is there, I’m thinking perhaps it’s there to skip the middle white space (between the “hello” and “world” because that too is white space? So using “I” is merely telling the computer “focus on ‘this’ option and ‘that’ option” if that makes sense.
Perhaps I should ask more…what is “I” then there for, “g” I can understand…
ok…so this would then prove what I said regarding the options (prevents changing the middle white space), but then…the “or” is giving 2 alternatives (can’t pick both?)
THIS is what I’m stuck on…it’s either the one or the other…so the “or” is limiting the “g” character then actually, almost like a contradiction if that makes sense.
Pretty much like an “if…else” condition
No, this is not what is happening. The pattern is looking for either white space at the beginning of the string or white space at the end of the string. Because of the “g” global operator, the pattern matching does not stop once it finds one of these sub patterns, it continues on through the end of the string. So first the pattern matches any white space at the front of the string and then it matches any white space at the end of the string. That is why the replace method is replacing both white space at the front and end of the string using this one regex.
a) Oh…so if the global wasn’t there it’ll just replace the left and finished right?
b) So if I wanted to also change the middle white space I’d take the or operator out and leave global in right? This operator is pretty much actually telling the computer WHICH spaces to change, right?
I’ll admit, your last answer did help…quite a bit
This operator is actually quite useful
are you actually telling me…that if there was a line of the same pattern (say for example there were 100 of the same word ) within a string and the or operator was in the regex that the regex engine would only match what the or operator gives and not the rest of the string???
I’m not sure I understand what you are asking here. By default the regex stops after the first match. The global “g” flag tells the regex to not stop after the first match and find as many matches as possible in the entire string. This doesn’t really have anything to do with the or operator. The or operator just allows you to match either “this” or “that”.
If this didn’t answer your question then I think you will need to be a lot more specific about what you are asking, providing examples.
alright…I’m referring to “this” or “that”
for example: “word, word, word, word”, there are 4 "word"s here (in my question there is made mention of 100 but I’m not going to type out 100x "word"s right now (100 was just hypothetical anyway). My question is…with the “this” or “that” ("or"operator) you can choose now which “word” is to be matched using the or operator, for example you can mention in the or operator the first “word” and the last "word, the 2 in the middle won’t be matched because you didn’t make mention of them in “or” operator that you wanted them matched
does this make more sense?
You can match the first “word” with the pattern ^word and the last “word” with the pattern word$ and then you can match both in a single regex using the global “g” flag and the or operator:
/^word|word$/g
This is basically the same thing as finding white space at both the beginning and end of the string, you are just searching for a “word” instead of white space.
but tell me then, what is wrong with the following code?
/(^[\s+][\s+]$)/g it should work? in editor it shows one change, the left takes one space away
I don’t know if this regex works because I don’t know what you are trying to match. Can you be more specific about what you are trying to do here?
Also, I’m not sure [\s+] is doing what you think it is doing. A good place to test your regex’s and have them explained is regex101. Put your regex in there and see what the explanation is.
I tried using fiddle and it took one space away on left, the other side had no spaces taken away. What I’m trying to do is actually create character sets on both sides (left and right) instead of using the or operator
I’m still not exactly sure what you are trying to match. It might help to give an example.
The last regex you gave:
/(^[\s+][\s+]$)/gi
Will only match a string that is two characters long and each character must either be a white space or the plus sign. You don’t even need the g or i flags, they are doing nothing. I’ll leave it up to you to figure out why.
I’m thinking this will only concentrate on the spaces in the beginning and ones at the end, hence you don’t need global. If the “or” was there you’ll need global. I also deleted the “i” as well, for obvious reasons
I’m trying to “play around” with the spaces in the beginning of a string and the end of a string using square brackets and parenthesis in any given string to see if I can achieve around the same result (that is by illuminating spaces)