Try .replace(/ /g,"_");

Edit: or .split(' ').join('_') if you have an aversion to REs

Edit: John Resig said:

If you're searching and replacing through a string with a static search and a static replace it's faster to perform the action with .split("match").join("replace") - which seems counter-intuitive but it manages to work that way in most modern browsers. (There are changes going in place to grossly improve the performance of .replace(/match/g, "replace") in the next version of Firefox - so the previous statement won't be the case for long.)

Answer from Crescent Fresh on Stack Overflow
🌐
Reddit
reddit.com › r/regex › how to replace space with underscores using a regex in eplan?
r/regex on Reddit: How to replace space with underscores using a regex in EPLAN?
August 26, 2024 -

Hey, guys. I’m a total newbie when it comes to regex and have no idea what I’m looking at, so I’m asking for your help. How can I replace spaces with underscores using a regex in EPLAN?

Example string: "This is a test" --> "This_is_a _test"

I also have an image of something else I’ve done where I removed '&E5/' from the string so that only "011" was left.

In EPLAN:

Where there are a Source Text and Output Text, one can put RegEx expressions.

Solution:

Discussions

Chain regex to match underscore,space and caps
How can i chain regex to match caps,spaces and underscore in one regex?I can match spaces and underscore using: str.replace(/[\s,’_’]/g,’-’); More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
4
0
April 26, 2019
How to replace underscores with spaces using a regex in Javascript - Stack Overflow
How can I replace underscores with spaces using a regex in Javascript? var ZZZ = "This_is_my_name"; More on stackoverflow.com
🌐 stackoverflow.com
How to convert all the spaces in an input to underscores
Here for this input i want to covert all the spaces to underscores Kindly guide More on forum.bubble.io
🌐 forum.bubble.io
8
0
February 21, 2023
Help replacing spaces with underscores and limiting the amount of underscores in Fibery
I think I figured it out, just needed to essentially reverse my groups. So first clear out all characters that are not spaces or underscores, then replace any spaces and underscores with a single underscore. Lower( ReplaceRegex( ReplaceRegex(Hello. I'm "___BOB___"! I'm feeling happy / healthy, "[^a-zA-Z0-9_\s]+", ""), "[\s_]+", "_" ) ) More on reddit.com
🌐 r/regex
6
1
July 24, 2024
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-replace-spaces-with-underscores
How to Replace Spaces with Underscores in JavaScript | bobbyhadz
March 1, 2024 - Use the `String.replaceAll` method to replace all spaces with underscores in a JavaScript string, e.g. `string.replaceAll(' ', '_')`.
🌐
GeeksforGeeks
geeksforgeeks.org › replacing-spaces-with-underscores-in-javascript
Replacing spaces with underscores in JavaScript | GeeksforGeeks
June 20, 2023 - Example 1: This example replaces all spaces(' ') with underscores("_") by using replace() method.
🌐
TutorialsPoint
tutorialspoint.com › replacing-spaces-with-underscores-in-javascript
Replacing spaces with underscores in JavaScript?
January 18, 2023 - <!DOCTYPE html> <html> <body> <h3>Replace Spaces with Underscores</h3> <p id="original"></p> <p id="result"></p> <button onclick="replaceSpaces()">Replace Spaces</button> <script> let sentence = "Welcome to Tutorials Point"; document.getElementById("original").innerHTML = "Original: " + sentence; function replaceSpaces() { let result = sentence.replace(/ /g, "_"); document.getElementById("result").innerHTML = "Result: " + result; } </script> </body> </html>
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Chain regex to match underscore,space and caps
April 26, 2019 - How can i chain regex to match caps,spaces and underscore in one regex?I can match spaces and underscore using: str.replace(/[\s,’_’]/g,’-’);
🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 26244 › regular-expression-find-and-replace-substring-further-replacing-all-spaces-in-only-defined-substring-with-underscores
Regular Expression - find and replace substring - further replacing all spaces in only defined substring with underscores | Notepad++ Community
October 27, 2024 - NOTE: -the first part of each string ‘\1’ is fixed length <— (^13[ 0-9]{51}) finds the first part -the second part of the strings ‘\2’ are the parts that have whatever characters with spaces where spaces need to be replaced with “_” <— (*) finds this second part -the third part of the strings ‘\3’ have variable lengths and alphanumeric characters <— ( [0-9]{3} [0-9]{2,22} [DC] *^13) seems to do the trick
Find elsewhere
🌐
CodeSpeedy
codespeedy.com › home › replace spaces with underscores in javascript
Replace spaces with underscores in JavaScript - CodeSpeedy
November 2, 2022 - That’s why to make the regex match all the whitespace the 'g' (global) flag has been added to the regular expression. ... If you remove the 'g' flag then only the first whitespace of the string will be replaced with the underscore.
🌐
Java2Blog
java2blog.com › home › core java › how to replace space with underscore in java
How to Replace Space with Underscore in Java - Java2Blog
September 17, 2022 - If you want to replace consecutive spaces with one underscore, you can use replaceAll with regex \\s+.
🌐
Code Highlights
code-hl.com › home › javascript › tutorials
5 Quick Methods to Replace Space with Underscore JavaScript | Code Highlights
August 18, 2024 - This code uses the replace method with a regular expression to find spaces and replace them with underscores. The most straightforward way to replace spaces is by using the replace() method. Here’s how it works: 1string.replace(searchValue, newValue); Run · searchValue: The value to search for (can be a string or regex).
🌐
Bubble
forum.bubble.io › need help
How to convert all the spaces in an input to underscores - Need help - Bubble Forum
February 21, 2023 - Here for this input i want to covert all the spaces to underscores Kindly guide
🌐
Tutor Python
tutorpython.com › python-replace-space-with-underscore
Python Replace Space with Underscore - Tutor Python
October 19, 2023 - The first argument, r"\s", represents a regular expression pattern that effectively matches any whitespace character. This pattern allows us to identify and locate all the spaces within the input string.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript replace space with underscore
How to Replace Space With Underscore in JavaScript | Delft Stack
February 2, 2024 - It will find out the first " " (space) in the string and replace it with "_" (underscore). We provided regex (regular expression) to replace all the spaces in the first argument.
🌐
Reddit
reddit.com › r/regex › help replacing spaces with underscores and limiting the amount of underscores in fibery
r/regex on Reddit: Help replacing spaces with underscores and limiting the amount of underscores in Fibery
July 24, 2024 -

I'm using Fibery to manage a bunch of business processes and trying to build a formula that uses their ReplaceRegex function, but struggling to achieve what I want.

ChatGTP keeps giving me solutions that don’t seem to work in Fibery’s approved RegEx format. I'm not entirely sure what they accept but they do link to this page in their documentation: https://medium.com/tech-tajawal/regular-expressions-the-last-guide-6800283ac034

If the input was:

Hello. I'm "___BOB___"! I'm feeling happy / healthy

I want the output to be:

hello_im_bob_im_feeling_happy_healthy

So basically:

  • All spaces should be replaced with underscores

  • All special characters (except for underscores) should be removed

  • There should never be more than 1 underscore in a row in the final output

I’ve got it mostly working with the following

Lower(
ReplaceRegex(
ReplaceRegex(
"Hello.  I'm "___BOB___"! I'm feeling happy / healthy", "[\s_]+", "_"),
"[^a-zA-Z0-9_]", "")
)

but it still spits out the following (based on my example):

hello_im__bob__im_feeling_happy__healthy

As you can see there’s a few spots that have double underscores.

How can I ensure the final output doesn’t have more than 1 underscore in a row? I know there's probably no Fibery experts here, but figured it was worth a shot...appreciate any help that could be provided.

🌐
Stack Exchange
wordpress.stackexchange.com › questions › 307438 › replace-dash-and-underscore-with-space
plugins - Replace Dash (-) and Underscore ( _ ) with Space - WordPress Development Stack Exchange
I want to replace dashes & underscores with space in this image file name: text1_text2-10212-etc_125.jpg (result of a plugin) My code is: $string = preg_replace('/[\-_]/',' ', $file['name']);...
Top answer
1 of 3
2

As it so happens, I wrote a snippet a little while ago to replace spaces with underscores within a selection:

<snippet>
    <content><![CDATA[${SELECTION/\s/_/g}]]></content>
</snippet>

Save this as Packages/User/replace_space_with_underscore.sublime-snippet. Then, open Preferences -> Key Bindings - User and add the following shortcut:

{ "keys": ["ctrl+shift+-"], "command": "insert_snippet", "args": { "name": 
    "Packages/User/replace_space_with_underscore.sublime-snippet" } }

If this is your only custom key binding, you'll have to surround it with [] square brackets, so it looks like this:

[
    { "keys": ["ctrl+shift+-"], "command": "insert_snippet", "args": { "name": 
        "Packages/User/replace_space_with_underscore.sublime-snippet" } }
]

Now, you can highlight URLs containing spaces, hit CtrlShift-, and they'll be replaced with underscores. Definitely more work than a regex, but if Phillip Schmidt's answer is true this may be the best way to do it. Good luck!

2 of 3
2

Provided that your tags are all images, with a similar format to the tags above you can acomplish this in a single regular expression using positive and negative lookaheads.

\s(?=(\w+))(?!style|alt|src|\d+px)

Positive Lookahead

The first step in this regular expression is to find spaces followed by one or more alphanumeric characters.

\s(?=(\w+))

This expression will find following matches:

  • Space between "img" and "style"
  • Space between "width" and "800px"
  • Space before src
  • Spaces in the filename
  • Space between filename and alt

The outer brackets allow of this regexp mean that the resulting matches will feed into the next portion of the expression i.e. the negative lookahead.

Negative Lookahead

This step negates the matches we are not interested in:

(?!style|alt|src|\d+px)

Another alternative, would be to use two positive lookaheads. This approach would give you a final regexp which looks like this:

\s(?=(\w+))(?=\w+(\s|\w)*\.[jpg])

The first portion of this expression is the same as above. The second lookahead is a bit more generic, looking for an alphanumeric character to start, followed by more alphanumeric characters or spaces multiple times followed by the .jpg extension.

Second Positive Lookahead

(?=\w+(\s|\w)*\.[jpg])
🌐
Blogger
javahungry.blogspot.com › 2023 › 05 › replace-space-with-underscore.html
Replace Space with Underscore in Java [2 ways] | Java Hungry
You can use regex "\\s+" in the replaceAll() method to achieve the desired result as shown below in the example. public class ReplaceSpaceWithUnderscore3 { public static void main(String args[]) { String str = "Be in Present"; str = str.replaceAll("\\s+ ","_"); System.out.println(str); } } ...
🌐
Java2Blog
java2blog.com › home › php › replace space with underscore in php
Replace Space with Underscore in PHP [2 ways] - Java2Blog
December 5, 2022 - This approach is useful when you want to remove multiple spaces with single underscore. ... We used preg_replace() with regular expression /\s+/ to replace space with underscore.
Top answer
1 of 2
8

The following worked for me:

rename 's/\s+/_/g' *

It will match one to unlimited instances of white space

Note this would also work for newlines and tabs, however based on your use case I think that would be preferable and not unwanted? But to match only space specifically you could do:

rename 's/ +/_/g' *
2 of 2
1

IDK if rename can work recursively. I made some empty files with space(s) in their names inside a subdirectory without a space and one with. The below works for me to rename files with one or more spaces in their names with one underscore, and doesn't bother the subdirectory with spaces in its name.

root@server <1>: /cwd# mkdir subdir1 'sub dir 2' ; touch 'subdir1/file 1' 'sub dir 2/file  2  ' 'subdir1/ f i l e 3 ' 'sub dir 2/   f  i  l  e   4   '

root@server <2>: /cwd# ls -lhR
.:
total 0
drwxr-xr-x 2 root root 80 Dec 28 00:42  subdir1
drwxr-xr-x 2 root root 80 Dec 28 00:42 'sub dir 2'

./subdir1:
total 0
-rw-r--r-- 1 root root 0 Dec 28 00:42 'file 1'
-rw-r--r-- 1 root root 0 Dec 28 00:42 ' f i l e 3 '

'./sub dir 2':
total 0
-rw-r--r-- 1 root root 0 Dec 28 00:42 'file  2  '
-rw-r--r-- 1 root root 0 Dec 28 00:42 '   f  i  l  e   4   '

root@server <3>: /cwd# find . -type f -exec echo {} \; | tee ../FILES.txt
./sub dir 2/   f  i  l  e   4
./sub dir 2/file  2
./subdir1/ f i l e 3
./subdir1/file 1

root@server <4>: /cwd# while IFS= read line ; do
> dirname="${line%/*}"
> fn=${line##*/}
> fn="${fn//+( )/_}"
> mv -v "$line" "${dirname}/${fn}"
> done < ../FILES.txt
renamed './sub dir 2/   f  i  l  e   4   ' -> './sub dir 2/_f_i_l_e_4_'
renamed './sub dir 2/file  2  ' -> './sub dir 2/file_2_'
renamed './subdir1/ f i l e 3 ' -> './subdir1/_f_i_l_e_3_'
renamed './subdir1/file 1' -> './subdir1/file_1'

root@server <5>: /cwd# ls -lhR
.:
total 0
drwxr-xr-x 2 root root 80 Dec 28 00:42  subdir1
drwxr-xr-x 2 root root 80 Dec 28 00:42 'sub dir 2'

./subdir1:
total 0
-rw-r--r-- 1 root root 0 Dec 28 00:42 file_1
-rw-r--r-- 1 root root 0 Dec 28 00:42 _f_i_l_e_3_

'./sub dir 2':
total 0
-rw-r--r-- 1 root root 0 Dec 28 00:42 file_2_
-rw-r--r-- 1 root root 0 Dec 28 00:42 _f_i_l_e_4_

Some notes on each command:

  1. creates two subdirectories, where the second has spaces in its name.

  2. lists the contents recursively. note the single quotes are used on files or folders having spaces.

  3. use find to find files (-type f) and echo the name to "FILES.txt" in the parent dir.

  4. use while loop to iterate over each line in the file. IFS= clears the input field separator so leading and trailing whitespace isn't truncated.

    4.1 extracts the directory name, which shouldn't be modified

    4.2 gets the file name

    4.3 replaces one or more consecutive space chars with a single underscore in the file name

    4.4 verbosely renames the files, but not the folders.

  5. lists the contents recursively. note the single quotes are used on files or folders having spaces.

I used parameter expansion for getting the dir names and file names instead of dirname and basename because it's faster.