Is there any reason the following string should fail the regular expression below?
String: "http://devices/"
Expression:
/^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+\.)/.test(input.val())
Thank you for your consideration.
Yes it will fail because of the last dot . in your regular expression.
/^ ... \.)/
^^
There is not one in the string you are validating against.
http://devices
^ Should be a dot, not a forward slash
Live Demo
If you are planning on using regex to do this, I would probably prefer using a RegExp Object to avoid all the escaping, or group the prefixes together using a non-capturing group.
/^((?:https?|ftp|pop|imap):\/{2}|www\.) ... $/
The last character in the string must be a period. see "\." at the end of the regex.
You can use http://rubular.com/ to test simple regex expressions and what their matches are.
The reason why it's failing is because, you are using:
^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+\.)
and you should use:
^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+.)
You don't have to escape . --------^
You need to close the regex with a $.
On this two last: .), this dot should be optional, as it is needed to validade.
to satisfy this "http://devices/" the regex in java at least is:
^((http://)|(https://)|(ftp://)|(pop://)|(imap://)){1}(www.)?([0-9A-Za-z]+)(.)?([0-9A-Za-z]+)?$
Are those / at the beggining and the end code delimiters?
Related
What I am trying is to match until first occurrence of & met. Right now it is matching only the last occurrence of &.
My regular expression is
(?!^)(http[^\\]+)\&
And I'm trying to match against this text:
https://www.google.com/url?rct3Dj&sa3Dt&url3Dhttp://business.itbusinessnet.com/article/WorldStage-Supports-Massive-4K-Video-Mapping-at-Adobe-MAX-with-Christie-Boxer-4K-Projectors---4820052&ct3Dga&cd3DCAEYACoTOTEwNTAyMzI0OTkyNzU0OTI0MjIaMTBmYTYxYzBmZDFlN2RlZjpjb206ZW46VVM&usg3DAFQjCNE6oIhIxR6qRMBmLkHOJTKLvamLFg
What I need is:
http://business.itbusinessnet.com/article/WorldStage-Supports-Massive-4K-Video-Mapping-at-Adobe-MAX-with-Christie-Boxer-4K-Projectors---4820052
Click for the codebase.
Use the non-greedy mode like this:
/(?!^)(http[^\\]+?)&/
// ^
In non-greedy mode (or lazy mode) the match will be as short as possible.
If you want to get rid ot the & then just wrap it in a lookahead group so it won't be in the match like this:
/(?!^)(http[^\\]+?)(?=&)/
// ^^ ^
Or you could optimize the regular expression as #apsillers suggested in the comment bellow like this:
/(?!^)(http[^\\&]+)/
Note: & is not a special character so you don't need to escape it,
I have done something like this
but its not working
can anyone please correct following regex.
/^[a-zA-Z.\s]+$/
You can use
/^[a-zA-Z]*$/
Change the * to + if you don't want to allow empty matches.
References:
Character classes ([...]), Anchors (^ and $), Repetition (+, *)
The / are just delimiters, it denotes the start and the end of the regex. One use of this is now you can use modifiers on it.
If you want to get only alphabets, remove . from regex. This will match all the alphabets and spaces.
/^[a-zA-Z\s]+$/
I'll also recommend you to use instead of \s
/^[a-zA-Z ]+$/
so that, other space characters(tabs, etc.) will not matched.
I'm trying to write a regular expression that will match a strings similar to the ones below:
Yu MSBE26
w AWAQBNL
I am using Javascript and have come up with the following regular expression:
(.*?(?:[AWMS\d]{2})[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})
In words, I start my capture group off by matching everything until the [AWMS\d]{2} pattern is encountered, then I match the [AWMS\d]{2} pattern, the [A-Z]{2} that follows and finally the [\dA-Za-z]{1,3} to match the final two or three characters.
From what I have read, this should be working, but I'm not getting any matches.
For example when I use a regex tester I don't get any matches: Sample
Remove the second [AWMS\d]{2} - it looks like an accidental addition and is the reason your regex doesn't work:
(.*?(?:[AWMS\d]{2})[A-Z]{2}[\dA-Za-z]{1,3})
Edit: you don't even need the non capture group, the square brackets are enough:
(.*?[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})
Your regex doesn't match your values because simply they don't match.
Your pattern is:
(.*?(?:[AWMS\d]{2})[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})
Yu MSBE26
^--- fails here
w AWAQBNL
^--- fails here
Btw, you can use your regex to match your strings as this:
(.*?[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})
Working demo
Tried to search for /\,$/ online, but coudnt find anything.
I have:
coords = coords.replace(/\,$/, "");
Im guessing it returns coords string index number. What I have to search online for this, so I can learn more?
/\,$/ finds the comma character (,) at the end of a string (denoted by the $) and replaces it with empty (""). You sometimes see this in regex code aiming to clean up excerpts of text.
It's a regular expression to remove a trailing comma.
That thing is a Regular Expression, also known as regex or regexp. It is a way to "match" strings using some rules. If you want to learn how to use it in JavaScript, read the Mozilla Developer Network page about RegExp.
By the way, regular expressions are also available on most languages and in some tools. It is a very useful thing to learn.
That's a regular expression that finds a comma at the end of a string. That code removes the comma.
// defines a JavaScript regular expression, used to match a pattern within a string.
\,$ is the pattern
In this case \, translates to ,. A backslash is used to escape special characters, but in this case, it's not necessary. An example where it would be necessary would be to remove trailing periods. If you tried to do that with /.$/ the period here has a different meaning; it is used as a wildcard to match [almost] any character (aside for some newlines). So in this case to match on "." (period character) you would have to escape the wildcard (/\.$/).
When $ is placed at the end of the pattern, it means only look at the end of the string. This means that you can't mistakingly find a comma anywhere in the middle of the string (e.g., not after help in help, me,), only at the end (trailing). It also speeds of the regular expression search considerably. If you wanted to match on characters only at the beginning of the string, you would start off the pattern with a carat (^), for instance /^,/ would find a comma at the start of a string if one existed.
It's also important to note that you're only removing one comma, whereas if you use the plus (+) after the comma, you'd be replacing one or more: /,+$/.
Without the +; trailing commas,, becomes trailing commas,
With the +; no trailing comma,, becomes no trailing comma
I am trying to learn javascript
i have this code:
x=x.replace(/^\s+|\s+$/g,"");
can i have a description about /^\s+|\s+$/g,""
searching to replace what with what ?
Regards
This is a regular expression. Basically \s matches whitespac characters and replaces them with "".
edit:
/ ... / marks the regex.
^\s+ Take 1or more whitespaces at the beginning of the string.
\s+$ Take 1 or more whitespaces at the end of the string.
/g Dont stop at the first match, but find all matches - "global flag"
It's removing whitespaces, either at the beginning or the end of the string, by replacing them with the empty string "".
You can find more about regular expressions here http://www.w3schools.com/jsref/jsref_obj_regexp.asp.
first, look at this: http://www.w3schools.com/jsref/jsref_replace.asp and then do not use stackoverflow for questions of this kind