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
Related
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?
I saw the other posts but none of them help me ...
So, i tried to match url in a string in javascript with regex it works perfectly on regex101 but fails in javascript.
var matches = feed.content.match(
'/((http|https|ftp):\/\/([a-zA-Z0-9\.\-\_\%]+\/?){1}([a-zA-Z0-9\.\-\_]+\/?)*(\?[a-zA-Z0-9\.\-\_\%\+\=\&\:]*)*)/ig'
);
And firebug returns me
SyntaxError: invalid quantifier
Please can you help me ?
As pointed out in the comments, you should remove the single quotes enclosing the regex. As well as that, I would propose making a few changes to the expression itself:
((https?|ftp):\/\/([\w.%-]+\/?)([\w.-]+\/?)*(\?[\w.%+=&:-]*)*)
The ? after the smeans that it is optional, so http and https will both match. \w is the word character class, so that covers A-Za-z0-9_ much more concisely. There's no need to escape all the symbols but a useful trick is to put the - at the end of the character class, so that it isn't interpreted as a range between two characters. The {1} isn't necessary as that's the default behaviour.
updated on regex101
You're passing the regex as a string - just get rid of the outer quotes.
var matches = feed.content.match(
/((http|https|ftp):\/\/([a-zA-Z0-9\.\-\_\%]+\/?){1}([a-zA-Z0-9\.\-\_]+\/?)*(\?[a-zA-Z0-9\.\-\_\%\+\=\&\:]*)*)/ig
);
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 looking for a regex that will give me the index of the last space in a string using javascript.
I was using goolge to find a suitable regex, but no success.
Even the SO-Question Regex to match last space character does not hold a solution because the goal there was to remove more than one character in the end.
What is the correct regex?
As I commented I would just use lastIndexOf() but here is a regex solution:
The regex / [^ ]*$/ finds the last space character in a string. Use it like this:
// Alerts 9
alert("this is a str".search(/ [^ ]*$/));
The correct solution is not using a regex at all but the built-in lastIndexOf method strings have. Regexes are meant to match strings, not give you indexes (even though grouped matchs may be returned as index+length instead of a string - C-based regex libraries usually do so to avoid unnecessary copying)
I have this regex which looks for %{any charactering including new lines}%:
/[%][{]\s*((.|\n|\r)*)\s*[}][%]/gm
If I test the regex on a string like "%{hey}%", the regex returns "hey" as a match.
However, if I give it "%{hey}%%{there}%", it doesn't match both "hey" and "there" seperately, it has one match—"hey}%%{there".
How do I make it ungreedy to so it returns a match for each %{}%?
Add a question mark after the star.
/[%][{]\s*((.|\n|\r)*?)\s*[}][%]/gm
Firstly, to make a wildcard match non-greedy, just append it with ? (so *? instead of * and +? instead of +).
Secondly, your pattern can be simplified in a number of ways.
/%\{\s*([\s\S]*?)\s*\}%/gm
There's no need to put a single character in square brackets.
Lastly the expression in the middle you want to capture, you'll note I put [\s\S]. That comes from Matching newlines in JavaScript as a replacement for the DOTALL behaviour.
Shorter and faster working:
/%\{([^}]*)\}%/gm