I need to hyphenate a string in javascript. The string is a url (e.g '/home/about/').
My current regex, is working but the output is not as desired.
If the first/last character of the string is a special character, it should be removed and instead of being changed into a hyphen.
Example:
var string = '/home/about/';
string.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase();
// Returns -home-about- but I need home-about
^\/ means / at begin and \/$ means / at the end. joined them with pipe to handle both removals from the end.
string = string.replace(/^\/|\/$/g, '').toLowerCase();
Then do your regex operation:
string.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase();
You can simply do this:
var s="/home/about/";
s.match(/[^\/]+/g).join('-'); // home-about
Instead of replace use finding groups.
Where You will look for a group of any characters prefixed and postfixed with any of the special characters (its only / or some more?).
Next concatenate '-' with that new string, and You are done.
Related
I want to replace only a part of the string of a regex pattern match. I found this answer but I don't get it...
How do I use substitution?
Example of what I want: keep the first slug digit, only replace others
/09/small_image/09x/ > /09/thumbnail/
1st: unknown digit
2nd: "small_image"
3rd: unknown digit + "x"
Here is what I have so far:
var regexPattern = /\/\d\/small\_image\/\d*x/;
var regexPattern = /\/\d\/(small\_image\/\d*x)$1/; ??
var result = regexPattern.test(str);
if (result) {
str = str.replace(regexPattern, 'thumbnail');
}
var input = "/09/small_image/09x/";
var output = input.replace(/(\/\d+\/)small_image\/\d*x/, "$1thumbnail");
console.log(output);
Explanation:
Put the part you want to keep in parentheses, then refer to that as $1 in the replacement string - don't put $1 in your regex. So (\/\d+\/) means to match a forward slash followed by one or more digits, followed by another forward slash.
(Note that you don't need to escape underscores in a regex.)
Go with
var regexPattern = /(\/\d+\/)small\_image\/\d*x/;
and
str = str.replace(regexPattern, '$1thumbnail');
First, you were missing the +. Because 09 are two digits, you need the regexp to match one or more digits (\ḑ would be exactly one). This is accomplished by \d+
Second, everything you match is being removed at first. To get the /09/ part back afterwards, you have to remember it by putting it into brackets in the regexp (...) and afterwards reference it in the replacement via $1
One could as well create other groups and reference them by $2,$3 ...
I need to check, if the string is valid hexadecimal number in 0x3f (for example) format.
var strRegex="0[xX][0-9a-fA-F]+";
var re = new RegExp(strRegex);
if (!re.test(theAddress) {.... error alert stuff... }
As I have it now, it accept stuff like 0x3q and so on.
What regex expression should I use instead?
Put anchors arround the regex:
var strRegex = "^0[xX][0-9a-fA-F]+$";
I would put word boundaries \b around the pattern:
var re = new RegExp("\b0[xX][0-9a-fA-F]+\b");
If you miss them the string 0x3q would match too since it contains 0x3 which would match the pattern. Using \b it would only match 0x3 if it is surrounded either by a space, tab the begin or the end of the string ,, ; etc.
Others suggested to use the ^ and $ anchors but this will only work if the string contains exactly one hexadecimal value. The solution with word boundaries matches also strings which contain one (or more) hex values.
I have a username field in my form. I want to not allow spaces anywhere in the string. I have used this regex:
var regexp = /^\S/;
This works for me if there are spaces between the characters. That is if username is ABC DEF. It doesn't work if a space is in the beginning, e.g. <space><space>ABC. What should the regex be?
While you have specified the start anchor and the first letter, you have not done anything for the rest of the string. You seem to want repetition of that character class until the end of the string:
var regexp = /^\S*$/; // a string consisting only of non-whitespaces
Use + plus sign (Match one or more of the previous items),
var regexp = /^\S+$/
If you're using some plugin which takes string and use construct Regex to create Regex Object i:e new RegExp()
Than Below string will work
'^\\S*$'
It's same regex #Bergi mentioned just the string version for new RegExp constructor
This will help to find the spaces in the beginning, middle and ending:
var regexp = /\s/g
This one will only match the input field or string if there are no spaces. If there are any spaces, it will not match at all.
/^([A-z0-9!##$%^&*().,<>{}[\]<>?_=+\-|;:\'\"\/])*[^\s]\1*$/
Matches from the beginning of the line to the end. Accepts alphanumeric characters, numbers, and most special characters.
If you want just alphanumeric characters then change what is in the [] like so:
/^([A-z])*[^\s]\1*$/
I'm trying to get rid of the slash character in case it exists at the end of my string. I used the following expression, intending to match any character not being slash at the end of the line.
var str = "http://hazaa.com/blopp/";
str.match("[^/$]+", "g");
For some reason (surely logical and explainable but not graspabled to me on my own), I get the split into three string looking as follows.
["http:", "hazaa.com", "blopp"]
What am I assuming wrongly?
How to resolve it?
In str.match("[^/$]+", "g");, why put dollar sign inside bracket? It's supposed to be outside, namely, str.match("[^/]+$", "g");.
To remove all the trailing slash, you can use str.replace(/\/+$/, ""). (If you'd like to remove the last trailing slash ONLY, remove the + in the replace's regex)
Update:
One more way that doesn't use replace:
function stripEndingSlashes(str) {
var matched = str.match("(.*[^/]+)/*$");
return matched ? matched[1] : "";
}
The regexp is choosing "everything except slash". That is why match() returns the parts of the string between slashes.
You can resolve it with the replace() function:
var str = "http://hazaa.com/blopp/";
//replace the last slash with an empty string to remove it
str = str.replace(/\/$/,'');
The regexp literal should always be surrounded between / characters. So here the regexp is:
\/ : this means a single slash character. In order to prevent Javascript from interpreting your slash as the end of regexp, it needs to be 'escaped' with a backslash.
$ : this means the end of the string
Your current regex will match the portion of string until the first / or $ is encountered. The second parameter is ignored; there is no second parameter for String.match.
To remove the trailing slash, use the String.replace function:
var str = "http://hazaa.com/blopp/";
str = str.replace(/\/$/, "");
console.log(str);
// "http://hazaa.com/blopp"
If you need to check whether a string ends with a slash, use the String.match method like this:
var str = "http://hazaa.com/blopp/";
var match = str.match(/\/$/);
console.log(match);
// null if string does not end with /
// ["/"] if string ends with a /
If you need to grab every thing except the last character(s) being /, use this:
var r = /(.+?)\/*$/;
console.log("http://hazaa.com/blopp//".match(r)); // ["http://hazaa.com/blopp//", "http://hazaa.com/blopp"]
console.log("http://hazaa.com/blopp/".match(r)); // ["http://hazaa.com/blopp/", "http://hazaa.com/blopp"]
console.log("http://hazaa.com/bloppA".match(r)); // ["http://hazaa.com/bloppA", "http://hazaa.com/bloppA"]
The 2nd index in the returned array contains the desired portion of the URL. The regex works as follows:
(.+?) un-greedy match (and capture) any character
\/*$ matches optional trailing slash(es)
The first portion regex is intentionally changed to un-greedy. If it was greedy, it would attempt to find the biggest match as long the the whole regex matches (consuming the trailing / in the process). When ungreedy, it will find the smallest match as long as the whole regex matches.
Why use regex? Just check if the last symbol of the string is a slash and then slice. Like this:
if (str.slice(-1) === '/') {
str = str.slice(0, -1);
}
I'm trying to use the following code with jQuery to validate hex value strings but I get unexpected results:
var a = new RegExp("0x[a-fA-F0-9]+")
var result = a.test('0x1n')
In this case, result actually returns true. What am I missing here?
You need anchors to match the beginning and the end of the string. This will make the regular expression try to match against the entire string instead of just a part of the string:
var a = new RegExp("^0x[a-fA-F0-9]+$")
Otherwise your regular expression matches the 0x1 part and returns true.
On another note, the following would be a little better:
var re = /^0x[a-f0-9]+$/i;
The i flag makes it case insensitive so you don't have to specify a-f and A-F.
Your regex does match that string, because you don't have any anchors on it. If you change your regex to ^0x[a-fA-F0-9]+$, then the string 0x1n will not match.
Edit: To further explain why your string matches, your regular expression is actually trying to match a string that contains 0x followed by one or more characters in the [a-fA-F0-9] character class. The string 0x1n contains 0x followed by 1, which is in the [a-fA-F0-9] character class.
Adding anchors means that your string must start with 0x, then finish with one or more characters in the [a-fA-F0-9] character class. 0x1n would fail to match, since it ends in an n, which is not in that character class.
It returns true because you're not requiring the entire string to match that pattern. Try this:
var a = new RegExp("^0x[a-fA-F0-9]+$")