by RegexNinja » Thu May 28, 2020 10:47 am
Jakov
Trm2 is correct, I've learned some things from that regex-section myself..
You might have figured it out already, but the regex-syntax for javascript's .replace() is:
.replace(/__RegexMatch__/modifiers, '__Replace__')
The underscores are thrown-in just for clarity..
Modifiers need not exist, but can be things like g(global/all), i(case-insensitive), or gi(for both).
Expressions within RegexMatch are where the manual really comes in handy..
Basically though, you just (group) any text that you wanna keep..
If you specify anything that's not within a (group), it means you wanna lose it.
Line1: a = name.replace(/^(.*)[-_\. ](x64|x86)(.*)$/, '$1|$2$3')
Sets the variable called 'a' to hold the results of what happens to OrigName after the replacement.
Match: Beginning(text) until the last [either: -_. ](x64-or-x86) and then (anything-else) until end-of-name.
The $#'s in Replace reference the (groups) in RegexMatch that you wanna keep..
If you decide to add any chars within the set: [-_\. ], make sure that its after the -
The - is a special-character when its in a [set], unless its the 1st-character.
This lets you use things like [a-f] to represent either: a, b, c, d, e or f.
You could improve yours with [-_\. ]+ to fix names like: "AnyName.v123_-- _ --._.x86" if its an issue.
As you've prob noticed, there's other special-chars like . that must be spec'd like \.
Otherwise . will mean: any-character whatsoever!
Line2: b = a.replace(/([^\d])[\._]+/g, '$1 ')
Sets the variable 'b' to hold the results of what happens to a after the replacement.
Group1 matches one: NonNumber, followed by [any:._] 1-or-more times, g=global(all) to replace all-occurences.
This entire-match get replaced by $1Space, (where $1 represents the NonNumber matched by Group1).
With javascript's .replace, since nothing else was matched, nothing else gets touched.
That's different than how #1Regex works (you must match everything that you wanna keep).
Best way to learn, I think, is taking examples from the manual/etc, and trying them out in BRU.
For javascript, I use places like: https://eloquentjavascript.net to grab the syntax.
Then I come back into BRU's javascript to try out different things against my names.
I dont learn well from reading, so I usually have to experiment until it sinks in.
But dont try it till you look at the manual.. At least learn a few varNames like name, counter, etc.
I made that mistake myself, and it just ended up holding me back.
Hope it helps with any future editing.. Cheers!