-4
/route/some pic name.jpg

I need to find all white spaces beetwen route/ and .jpg using RexExp.

4 Answers4

0

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.

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
0

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);
0

You just need \s like /\s/g

/g will match all ocurrences of \s

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
Danu
  • 1
  • 1
0

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.

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161