If I search for How to Format i get
How%20to%20Format
but I want to be How-to-Format
window.location = '/search/'+encodeURIComponent(query);
also escape(), encodeURI() dont work
Try like this:
window.location = '/search/'+ query.replace(/\s+/gi, '-');
Working example with stripping multi-spaces into one:
http://jsfiddle.net/VVEpE/
encodeURIComponent (and the broken, deprecated escape) will convert space characters to representations of a space that you can put in a URI.
If you want to use custom slug generation rules then you will have to write them yourself, probably using a regular expression.
e.g.
query.replace(/\s/g, "-");
string.replace(searchvalue,newvalue)
in this case
window.location = '/search/'+query.replace(/ /g, '-')
Related
I am trying to write a regex which will do the following
If I have url like
example.com?mask=33&filter=23423&mode=12
It will match substring filter=23423
I need also to consider that the url can be
example.com?filter=23423
Thanks in advance.
You may just not consider [\?&] in your regexp and put /filter=[0-9]*/ if you sure that there will not be any params like other_filter=987 in your url.
If you may have such use substr on the resulted regexp like so
url.match(/[\?&]filter=[0-9]*/)[0].substr(1)
I have string like this: http://someurl.com?test&lettersg and I would like to mach part from first letter to & (without &, this part only: http://someurl.com?test).
var match = /^[^&]*/.exec("http://someurl.com?test&lettersg")[0];
If its the code you want, look at the source of this JQuery Plugin (URL Parser). Or you can just go ahead and use the plugin.
For URL manipulation I suggest using URI.js.
Your problem can be solved with and without RegExp:
var string = "http://someurl.com?test&lettersg";
console.log(string.match(/^[^&]+/)[0]);
console.log(string.substring(0, string.indexOf('&')));
Basically I want to be able to grab the ending of an url, and convert it into a string to be used somewhere.
Currently I'm doing this (which is less than optimal):
// grab the path, replace all the forward slashes with spaces
local_path = location.pathname.toString().replace(/\//g,' ');
// strip empty spaces from beginning / end of string
local_path.replace(/^\s+|\s+$/g,""));
But I think there is probably a better way. Help?
Edit: Could I confidently get rid of the .toString method there?
You could do something like this if you want to avoid regular expressions:
location.pathname.substring(1).split('/').join(' ')
That will get rid of the initial slash, but won't take care of a trailing slash. If you need to deal with those, you can omit substring and use trim for modern implementations or a regex:
location.pathname.split('/').join(' ').replace(/^\s+|\s+$/g, '')
What's wrong with what you have? Looks fine to me. That is the easiest way to handle what you want to do.
You could use the regex provided by Douglas Crockford on http://www.coderholic.com/javascript-the-good-parts/ and then split the path at the forward-slash.
I'm trying to remove a pound sign (£) from a string using javascript. I'm trying to do it using
str = str.replace(/\£/g, "");
However, it is not removing the sign.
The value of str is being fetched from a span (and the correct value is being fetched). This span has been previously set using javascript, with it being encoded in the string as
£
Any ideas on the best way to remove the pound sign?
You may need to use unicode for this. E.g., '£10.00'.replace(/\u00A3/g, '');
This way it works for me:
var str = "£sdfsdf";
str = str.replace("£", "");
alert(str);
Fiddle here: http://jsfiddle.net/peUrn/1/
Remove the backslash from your regexp.
You can just do
"hello w£orld".replace(/£/g,"")
The easy way is:
str.replace('£', '');
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character
Which means, in order to encode a pound sign, JavaScript uses 2 characters.
£ = %C2%A3
See http://fyneworks.blogspot.com/2008/06/british-pound-sign-encoding-revisited.html for more information.
It would be best to use %C2%A3 in place of the pound sign in your script.
I insert some strings in an array but before I do that I want to do what topic says. To only replace space with %20 I do:
Name.push(linkText.replace(" ", "%20"));
But how do I perform two "replace" there in one go?
Thanks in advance
It looks to me like you are trying to encode plaintext to use it in a URL or query string. I suspect you would be better off using one of javascript's built-in encoding methods, encodeURI or encodeURIComponent. See:
http://www.javascripter.net/faq/escape.htm
Do you want to replace two spaces in a row with one %20?
Name.push(linkText.replace(/ +/g, "%20"));
Or do you want to replace 2 spaces with %20%20?
Name.push(linkText.replace(/ /g, "%20"));
You could do something like this:
Name.push(linkText.replace(" ", "%20").replace("/", ""));
You can't do it using a single function call in regular JavaScript.
You could do this:
Name.push(linkText.replace(" ", "%20").replace("/", ""));