Try this System.Text.RegularExpressions.Regex.Replace(strOriginal, "[^""]loves[^""]", " hates ") [image] You will get something like this: [image] Answer from argin.lerit on forum.uipath.com
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ standard โ€บ base-types โ€บ substitutions-in-regular-expressions
Substitutions in Regular Expressions - .NET | Microsoft Learn
If this is not your intent, you can substitute a named group instead. For example, you can use the replacement string ${1}1 instead of $11 to define the replacement string as the value of the first captured group along with the number "1".
๐ŸŒ
UiPath Community
forum.uipath.com โ€บ help โ€บ community
How Replace a string in regex - Community - UiPath Community Forum
June 16, 2023 - Hello Community, I want to replace a word (loves) with another word (hates) by using regex, such that i just replace the text content and not the values in the Tags. Input: My dog loves dog food My dog loves dog food Output: My dog **hates** ...
Discussions

regex string replace
I am trying to do a basic string replace using a regex expression, but the answers I have found do not seem to help - they are directly answering each persons unique requirement with little or no More on stackoverflow.com
๐ŸŒ stackoverflow.com
RegEx - Find and Replace with Variables - Actions - Help & Questions - Drafts Community
I am trying to figure out how to use Find & Replace with RegEx to manipulate text. In this case, want to set a variable using RegEx and then replace text with the variable. Text: Mr. John Smith Mr. XX goes to town. Sโ€ฆ More on forums.getdrafts.com
๐ŸŒ forums.getdrafts.com
0
January 6, 2020
How to replace part of a string using regex
Because it should only replace the value in the second bracket area. ... and again a downvote on a question of mine in a short time - i've asked a total of 5 questions here on this platform and everyone of them got downvoted in the last couple days, at least provide an explanation ... ... I think you were downvoted because your question is not very clear. ... Save this answer. ... Show activity on this post. You may use a regex ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
regex - Regular expression replace in C# - Stack Overflow
I'm fairly new to using regular ... in my Regex.Replace formatted properly. Here's the scenario I'm working on... When I pull my data from the listbox, I want to format it into a CSV like format, and then save the file. Is using the Replace option an ideal solution for this scenario? Before the regular expression formatting example... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Microsoft Support
support.microsoft.com โ€บ en-us โ€บ office โ€บ regexreplace-function-9c030bb2-5e47-4efc-bad5-4582d7100897
REGEXREPLACE Function | Microsoft Support
Use REGEXREPLACE with capturing groups to separate and reorder given name and last name, using pattern: "([A-Z][a-z]+)([A-Z][a-z]+)"; and replacement: "$2, $1". Note: Capturing groups are defined in pattern with parentheses "()", and can be referenced in replacement as "$n". In this example, ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ en โ€บ middleware โ€บ enterprise-data-quality โ€บ 12.2.1.3 โ€บ edqoh โ€บ regex-replace.html
RegEx Replace
In this example, RegEx Replace is used to replace three digits followed by a space and <anything> with <anything><space><the three digits>.
Top answer
1 of 2
1

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 2 depending on the language you are using. This brings me to probably the easiest answer to your problem:

(.*)

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.

2 of 2
0
  • 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):

Find elsewhere
๐ŸŒ
Highbond
help.highbond.com โ€บ helpdocs โ€บ analytics โ€บ 15 โ€บ en-us โ€บ Content โ€บ analytics โ€บ scripting โ€บ functions โ€บ r_regexreplace.htm
REGEXREPLACE( ) function
... Returns the character field data with the spacing between words standardized on a single space. Using the BLANKS( ) function in new_string, rather than a literal space, makes spaces easier to read and less likely to be overlooked: ... Returns "(123) 456-7890".
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ replace
String.prototype.replace() - JavaScript | MDN
(Corresponds to $1, $2, etc. above.) For example, if the pattern is /(\a+)(\b+)/, then p1 is the match for \a+, and p2 is the match for \b+. If the group is part of a disjunction (e.g., "abc".replace(/(a)|(b)/, replacer)), the unmatched alternative will be undefined.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-string-replace-example-with-regex
JavaScript String.Replace() Example with RegEx
October 20, 2020 - To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/. This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.text.regularexpressions.regex.replace
Regex.Replace Method (System.Text.RegularExpressions) | Microsoft Learn
A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged. ... options is not a valid bitwise combination of RegexOptions values.
๐ŸŒ
Regular-Expressions.info
regular-expressions.info โ€บ replacetutorial.html
Replacement Text Tutorial
Escaping rules may get a bit complicated when using replacement strings in software source code. ... Non-printable characters such as control characters and special spacing or line break characters are easier to enter using control character escapes or hexadecimal escapes. ... Reinserting the entire regex match into the replacement text allows a search-and-replace to insert text before and after regular expression matches without really replacing anything.
๐ŸŒ
JetBrains
jetbrains.com โ€บ help โ€บ webstorm โ€บ tutorial-finding-and-replacing-text-using-regular-expressions.html
Find and replace text using regular expressions | WebStorm Documentation
June 17, 2026 - See RegEx syntax for more details. You can put the regular expressions inside brackets in order to group them. Each group has a number starting with 1, so you can refer to (backreference) them in your replace pattern. Note that the group 0 refers to the entire regular expression. However, you can refer to the captured group not only by a number $n, but also by a name ${name}. For example...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations
May 25, 2026 - However, Unicode strings and 8-bit strings cannot be mixed: that is, you cannot match a Unicode string with a bytes pattern or vice-versa; similarly, when asking for a substitution, the replacement string must be of the same type as both the pattern and the search string. Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Pythonโ€™s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal.
๐ŸŒ
Drafts Community
forums.getdrafts.com โ€บ actions - help & questions
RegEx - Find and Replace with Variables - Actions - Help & Questions - Drafts Community
January 6, 2020 - I am trying to figure out how to use Find & Replace with RegEx to manipulate text. In this case, want to set a variable using RegEx and then replace text with the variable. Text: Mr. John Smith Mr. XX goes to town. Script // define regex to use... const findRegex = /([M][r-s]\.\s)(\w*\s)(\w*)/g; // define replacement expression... const FirstName = "$2"; const LastName = "$3"; //Find Text const XX = "XX"; // // do the replacement... draft.content = draft.content.replace(XX, LastName); dra...
๐ŸŒ
Exceljet
exceljet.net โ€บ home โ€บ functions โ€บ regexreplace function
Excel REGEXREPLACE function | Exceljet
June 17, 2025 - Another way to replace both "T" and "t" with "b" is to include both in a regex character set like this: =REGEXREPLACE("Tuttle","[Tt]","b") // returns "bubble" The REGEXREPLACE function provides an easy way to remove non-numeric characters from ...
๐ŸŒ
DEV Community
dev.to โ€บ sparky โ€บ regular-expression-capture-and-replace-examples-44o5
Regular expression capture and replace examples - DEV Community
September 26, 2022 - string censored = regex.Replace( requestXml, "<BankAccountNumber>*********</BankAccountNumber>"); Console.WriteLine(censored); ... <Name>Sparky's Bank Account</Name> <BankRoutingNumber>123456789</BankRoutingNumber> <BankAccountNumber>******...
Top answer
1 of 4
22

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 $1 backreference 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).
2 of 4
1

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.

๐ŸŒ
Splunk Community
community.splunk.com โ€บ t5 โ€บ Splunk-Search โ€บ How-to-replace-a-string-with-RegEx-in-search-result โ€บ m-p โ€บ 601544
Solved: How to replace a string with RegEx in search resul... - Splunk Community
June 20, 2022 - I have my Sonicwall logfiles coming into Splunk. By searching this index I want to replace "dst" (Destination IP address) without portnumber and interface with (for example) RegEx. Note that the formats used for "src" and "dst" = (ip address):(port number):(interface) So when I do a search ...
๐ŸŒ
Alteryx Community
community.alteryx.com โ€บ t5 โ€บ Alteryx-Designer-Desktop-Discussions โ€บ Regex-to-Replace-Parts-of-a-String โ€บ td-p โ€บ 28463
Regex to Replace Parts of a String - Alteryx Community
June 13, 2024 - Hi, I searched and found posts but still struggling to get my head round an example. What I am trying to do is take the following entries in my data.... 2014_1 2014_2 2015_1 2015_2 2015_1 and make them read 2014(1) 2014(2) 2015(1) 2015(2) 2015(1) In essence replacing the "_" with"(" and plac...