regex string replace
RegEx - Find and Replace with Variables - Actions - Help & Questions - Drafts Community
How to replace part of a string using regex
regex - Regular expression replace in C# - Stack Overflow
This should work :
str = str.replace(/[^a-z0-9-]/g, '');
Everything between the indicates what your are looking for
/is here to delimit your pattern so you have one to start and one to end[]indicates the pattern your are looking for on one specific character^indicates that you want every character NOT corresponding to what followsa-zmatches any character between 'a' and 'z' included0-9matches any digit between '0' and '9' included (meaning any digit)-the '-' charactergat the end is a special parameter saying that you do not want you regex to stop on the first character matching your pattern but to continue on the whole string
Then your expression is delimited by / before and after.
So here you say "every character not being a letter, a digit or a '-' will be removed from the string".
Just change + to -:
str = str.replace(/[^a-z0-9-]/g, "");
You can read it as:
[^ ]: match NOT from the set[^a-z0-9-]: match if nota-z,0-9or-/ /g: do global match
More information:
- https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
In your case there are a couple of easy solutions. This first solution uses only basic regex:
[\.\w]+$
This captures every word character \w or period \. from the end $, stopping when it reaches any other type of character. This works because the space is not a word character or a period.
If you would like to select the region you want to remove, you can just use:
.*
Read this as match every character .* until the last space . The reason this matches until the last space, is because the star is a greedy quantifier. This means it matches everything it can, then starts giving back characters as needed for the rest of the pattern to match. To match until the first space, use .*? . The question mark next to the star makes the star lazy. This means the star will match as little as it has to in order for the next part of the expression to find a match.
However, capture groups are ideal for problems like this, where you would like to remove part of a line and keep the rest. A simple formula for this is:
^(.*)pattern_to_remove(.*)$
You can then recover the rest using backreferences. These store the capture groups (anything in parenthesis) from you regex into preset variables. In the pattern above, this would be \1\2 or depending on the language you are using. This brings me to probably the easiest answer to your problem:2
(.*)
The first capture group contains everything after the space. This regex is very straightforward and easy to read. Move to the first space , then capture everything after (.*).
For a comprehensive reference on regex, check out regular-expressions.info. Here is a link to the page on capture groups.
- Ctrl+H
- Find what:
^\S+\h+ - Replace with:
LEAVE EMPTY - CHECK Wrap around
- CHECK Regular expression
- Replace all
Explanation:
^ # beginning of line
\S+ # 1 or more non spaces
\h+ # 1 or more horizontal spaces
Screenshot (before):

Screenshot (after):

You may use a regex that will match the first [....] followed with [ and capture that part into a group (that you will be able to refer to via a backreference), and then match 1+ chars other than ] to replace them with your replacement:
var str = "asd[595442/A][30327][0]";
var strToReplace = "30333";
console.log(str.replace(/(\[[^\]]*]\[)[^\]]*/, "$1" + strToReplace));
var strDesiredResult = "asd[595442/A][30333][0]";
console.log(strDesiredResult);
The /(\[[^\]]*]\[)[^\]]*/ has no gmodifier, it will be looking for one match only.
Since regex engine searches a string for a match from left to right, you will get the first match from the left.
The \[[^\]]*]\[ matches [, then any 0+ chars other than ] and then ][. The (...) forms a capturing group #1, it will remember the value that you will be able to get into the replacement with $1 backreference. [^\]]* matches 0+ chars other than ] and this will be replaced.
Details:
(- a capturing group start\[- a literal[symbol (if unescaped, it starts a character class)[^\]]*- a negated character class that matches zero or more (due to the*quantifier)]- a literal](outside a character class, it does not have to be escaped)\[- a literal[
)- end of capturing group #1 (its value can be accessed with$1backreference from the replacement pattern)[^\]]*- 0+ (as the*quantifier matches zero or more occurrences, replace with+if you need to only match where there is 1 or more occurrences) chars other than](inside a character class in JS regex,]must be escaped in any position).
There are many ways to do this. One possible pattern is
str.replace(/^(.+)(\[.+\])(\[.+\])(\[.+\])$/, `$1$2[${strToReplace}]$4`)
You can see that $<number> is referred to captured string from regex (string groups in parentheses). We can refer to those and rearrange it however we want.
You can do it this with two replace's
//let stw be "John Smith $100,000.00 M"
sb_trim = Regex.Replace(stw, @"\s+\$|\s+(?=\w+$)", ",");
//sb_trim becomes "John Smith,100,000.00,M"
sb_trim = Regex.Replace(sb_trim, @"(?<=\d),(?=\d)|[.]0+(?=,)", "");
//sb_trim becomes "John Smith,100000,M"
sw.WriteLine(sb_trim);
Try this::
sb_trim = Regex.Replace(stw, @"(\D+)\s+\$([\d,]+)\.\d+\s+(.)",
m => string.Format(
"{0},{1},{2}",
m.Groups[1].Value,
m.Groups[2].Value.Replace(",", string.Empty),
m.Groups[3].Value));
This is about as clean an answer as you'll get, at least with regexes.
(\D+): First capture group. One or more non-digit characters.\s+\$: One or more spacing characters, then a literal dollar sign ($).([\d,]+): Second capture group. One or more digits and/or commas.\.\d+: Decimal point, then at least one digit.\s+: One or more spacing characters.(.): Third capture group. Any non-line-breaking character.
The second capture group additionally needs to have its commas stripped. You could do this with another regex, but it's really unnecessary and bad for performance. This is why we need to use a lambda expression and string format to piece together the replacement. If it weren't for that, we could just use this as the replacement, in place of the lambda expression:
"$1,$2,$3"