I would like to be able to get or detect all spaces of the %20
y type +
in the url, and replace them with -
using JavaScript.
I have this regex /[\s\%20\+]/g
but I don't know if it really works, I don't know much about regular expressions xD.
Example with +
:
http://miurl.com/mi+url+web
And I want it to be like this:
http://miurl.com/mi-url-web
Example with %20
:
http://miurl.com/mi%20url%20web
And I want it to be like this;
http://miurl.com/mi-url-web
Example with +
and %20
:
http://miurl.com/mi+url%20web
And I want it to be like this:
http://miurl.com/mi-url-web
Here I add my hashed urls:
http://miurl.com/#!mi+url%20web
And I handle them with the event hashchage
and normally I mean reloading the page completely manually, only when correcting the urls with -
it creates an infinite redirection. I created a code referring to the one shown here, but doing it with window.location.hash
makes me infinite redirect.
javascript
This is the code to replace the hash (fragment/label) of a URI. It can go in the
<head>
HTML so that it is the first thing to be executed.You can see that it is being replaced
Regex description:
\+
- A+
literal(more on special characters ).
|
- Alternation ::: works like aOR
(more on alternation ).
%20
-%20
Verbatim text ./g
- Modifierg
::: find all occurrences ( global ).Regex demo: https://regex101.com/r/R35zAs/1
Script demo: http://mariano.uphero.com/50400/#demo+del%20script
Other options to replace spaces:
/[+ ]|%20/g
- To also replace literal spaces./(?:[+ ]|%20)+/g
- To convert to multiple consecutive spaces in a single hyphen..htaccess
If you also want to replace the characters of the path ( pathname ) from the
.htaccess
, the code would be:Description
%20
, because Apache interprets them directly as spaces. Therefore,[+ ]
(a+
or a space) is replaced.The first rule replaces when there is more than one character, using the flag
[N]
that causes all the rules to be executed again (we generate a loop).^([^+ ]*)[+ ]
- Matches from the beginning with 1. any number of non-+
space characters (captured in$1
), and 2. with a character[+ ]
.([^+ ]*[+ ].*)
- Captures$2
1. more non-+
space characters, 2. one searched character, and 3. all the rest to the end./*$
- Matches a slash at the end of the URI, to leave it out of the group, and not include it in the rewrite (otherwise, trailing slashes would be accumulating for each replaced space).$1-$2
- Everything before the first+
or space, a-
and everything after.The second rule is the same, except that it only matches a single
+
or space (there can be 1 only because if it got this far, it didn't match the previous rule). In this case, instead of rewriting, we are redirecting with[R]
, thus changing the URL being accessed.Demo
http://mariano.uphero.com/50400/b/path+with%20spaces#and+hash%20also
I think with this simple function you can replace what you are looking for
A minified adaptation of the code in version 1 of Jorious 's answer
Explanation
/
, after which the global search modifier is indicatedg
.%20
is one of the sequence of characters, it is placed directly\+
the character has been escaped+
, which corresponds to the other sequence of characters because it is a special character|
it has been placed between the two previous expressions, to indicate one, the other or both ( logical disjunction operator ,ó
(OR) boolean)I thought that in the end I solved it, if I'm wrong about something I hope you help me correct it, I will be very grateful, this is my code:
In my .htaccess file in the rewrite rule I only allow only alphanumeric characters and "+" and "/", by including the "+" symbol I am allowing blank spaces, without the need to replace by "-" characters in the URL or to do any additional functions in php or javascript, since the %20 is a special character automatically encoded, by allowing it in the rewrite rule, it decodes it and passes it to "$1" saving you work.
RewriteRule ^([a-zA-Z0-9+/]+)$ index.php?views=$1
RewriteRule ^([a-zA-Z0-9+ <--- "+" blanks