Try .replace(/ /g,"_");
Edit: or .split(' ').join('_') if you have an aversion to REs
Edit: John Resig said:
Answer from Crescent Fresh on Stack OverflowIf 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.)
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.)
try this:
key=key.replace(/ /g,"_");
that'll do a global find/replace
javascript replace
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:
Chain regex to match underscore,space and caps
How to replace underscores with spaces using a regex in Javascript - Stack Overflow
How to convert all the spaces in an input to underscores
Help replacing spaces with underscores and limiting the amount of underscores in Fibery
If it is a JavaScript code, write this, to have transformed string in ZZZ2:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.replace(/_/g, " ");
also, you can do it in less efficient, but more funky, way, without using regex:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.split("_").join(" ");
Regular expressions are not a tool to replace texts inside strings but just something that can search for patterns inside strings. You need to provide a context of a programming language to have your solution.
I can tell you that the regex _ will match the underscore but nothing more.
For example in Groovy you would do something like:
"This_is_my_name".replaceAll(/_/," ")
===> This is my name
but this is just language specific (replaceAll method)..
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.
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!
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])
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' *
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:
creates two subdirectories, where the second has spaces in its name.
lists the contents recursively. note the single quotes are used on files or folders having spaces.
use
findto find files (-type f) and echo the name to "FILES.txt" in the parent dir.use
whileloop 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.
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.