This should work :

str = str.replace(/[^a-z0-9-]/g, '');

Everything between the indicates what your are looking for

  1. / is here to delimit your pattern so you have one to start and one to end
  2. [] indicates the pattern your are looking for on one specific character
  3. ^ indicates that you want every character NOT corresponding to what follows
  4. a-z matches any character between 'a' and 'z' included
  5. 0-9 matches any digit between '0' and '9' included (meaning any digit)
  6. - the '-' character
  7. g at 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".

Answer from koopajah on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › base-types › substitutions-in-regular-expressions
Substitutions in Regular Expressions - .NET | Microsoft Learn
They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters.
Discussions

How Replace a string in regex
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** dog food My dog **hates** dog food More on forum.uipath.com
🌐 forum.uipath.com
6
0
June 16, 2023
I'm somewhat of a coder myself
The capitalizing expert was what nearly killed me. More on reddit.com
🌐 r/ClaudeAI
132
6543
April 24, 2026
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):

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
The whole string being examined. ... An object whose keys are the used group names, and whose values are the matched portions (undefined if not matched). Only present if the pattern contains at least one named capturing group. The exact number of arguments depends on whether the first argument is a RegExp object — and, if so, how many capture groups it has. The following example will set newString to 'abc - 12345 - #$*%': ... function replacer(match, p1, p2, p3, offset, string) { // p1 is non-digits, p2 digits, and p3 non-alphanumerics return [p1, p2, p3].join(" - "); } const newString = "abc12345#$*%".replace(/(\D*)(\d*)(\W*)/, replacer); console.log(newString); // abc - 12345 - #$*%
🌐
Regular-Expressions.info
regular-expressions.info › replacetutorial.html
Replacement Strings Tutorial
A replacement string, also known as the replacement text, is the text that each regular expression match is replaced with during a search-and-replace. In most applications, the replacement text supports special syntax that allows you to reuse the text matched by the regular expression or parts ...
🌐
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** dog food My dog **hates** dog food
Find elsewhere
🌐
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.
🌐
W3Schools
w3schools.com › jsref › jsref_replace.asp
W3Schools.com
The replace() method searches a string for a value or a regular expression.
🌐
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 - Enter a search string in the top field and a replace string in the bottom field. Click to enable regular expressions. If you want to check the syntax of regular expressions, hover over and click the Show expressions help link. When you search for a text string that contains special regex symbols, WebStorm automatically escapes them with backlash \ in the search field.
🌐
Flexiple
flexiple.com › python › python-regex-replace
Python regex: How to search and replace strings | Flexiple - Flexiple
We can use regex to replace multiple patterns at one time using regex. This can be easily done using the following syntax. re.sub(pattern_1 | pattern_2, replacement, string, count=0, flags=0)
🌐
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.
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-replace-specific-words-with-another-word-in-a-string-using-regular-expressions
Replace Specific Words in a String Using Regular Expressions in JavaScript - GeeksforGeeks
July 23, 2025 - We iterate through the map object and use regular expressions to replace all occurrences of each word with its replacement in the string. JavaScript · function replaceWordsWithMap(str, replacements) { for (const [word, replacement] of ...
🌐
Browserling
browserling.com › tools › regex-replace
Regex Text Replacement - Substitute with Regular Expressions - Online - Browserling Web Developer Tools
World's simplest regular expression match replacer for web developers and programmers. Just paste your text in the form below, press Replace Regexp button, and you get replaced matches. Press button, get text.
🌐
Zod
zod.dev
Intro | Zod
import * as z from "zod"; const User = z.object({ name: z.string(), }); // some untrusted data... const input = { /* stuff */ }; // the parsed result is validated and type safe!
🌐
Reddit
reddit.com › r/claudeai › i'm somewhat of a coder myself
r/ClaudeAI on Reddit: I'm somewhat of a coder myself
April 24, 2026 - The real best way to do this is with a single line of Unicode-aware regex: /^\p{Lu}/u.test(str). One user provided a super detailed breakdown of why this beats all other simple methods, especially the 2022 example. So we all came for the laughs and left with a free coding lesson. Classic. ... Missed a curly brace.. ... Oh where? It isn't a real screenshot I had chatgpt generate it lol. I think there is also an extra " ... In the "text to analyze" multi-string prompt it's missing the beginning curly brace.
🌐
Atlassian Support
support.atlassian.com › atlassian support › jira service management › resources › search for issues in jira
Use advanced search with Jira Query Language (JQL) | Jira Service Management Cloud | Atlassian Support
String processing methods · Regular expressions for customizing and filtering alerts · Bulk add services to integration create alert rules · Integrate IT applications and tools · Add a global integration · Add an integration (team-level) Integrate with email ·
🌐
Substack
veedaily19.substack.com › veeraj’s substack › actually way to learn python to build projects + clear interviews.
Actually Way to Learn Python to build projects + Clear Interviews.
April 23, 2026 - I have always believed in one thing. That there is no point in watching tutorials or doing mini projects to learn something. Any programming language should be learnt from 2 perspective : · Clearing Interviewing
🌐
Traefik
plugins.traefik.io › plugins
Plugins
Plugin Catalog · Install a Plugin · Create a Plugin · Give Feedback · Traefik Labs. Terms of Services. Cookie Policy