/route/some pic name.jpg
I need to find all white spaces beetwen route/
and .jpg
using RexExp.
/route/some pic name.jpg
I need to find all white spaces beetwen route/
and .jpg
using RexExp.
You can just use this regular expression selector. Just make sure to use the global flag (/g
):
\s
Here is an example:
var text = "/route/some pic name.jpg";
var regex = /\s/g;
var replacement = "_";
var result = text.replace(regex, replacement);
console.log(result);
If you want to match only spaces, you can also just use a space as a selector together with the global flag
/g
.
Regexp:
\s <= 1 White Space
\s+ <= 1 or more White Space
\s* <= 0 or more White Space
const regex = /\s+/g;
const str = `/route/some pic name.jpg`;
const subst = `-`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
If you want to replace spaces with say "_" or if you want to count the number of spaces in the string, you can do something like this.
String pattern = "\\s+"; // regular expression for whitespaces
String str = "/route/some pic name.jpg";
System.out.println(str.replaceAll(pattern, "_"));
System.out.println(str.length() - str.replaceAll(pattern, "").length()); // prints 2
The above code snippet is in Java.