The following replacement
"index.html".replace('\.html$', '_fr.html');
returns "index.html", indicating that the first argument didn't match anything. However, if I remove the "$"
"index.html".replace('\.html', '_fr.html');
then the first argument matches and "index_fr.html" is returned.
Returning to the first example, can someone explain why ".html$" does not seem to match "index.html"?
Because that's not a regular expression - regex literals in JavaScript look like:
/\.html$/
without quotes. String.replace takes a string or a regular expression literal.
Related
If I want to find a reference to precisely the following string:
http ://www.mydomain.com/home
within a more complex regex expression.
Is it possible to escape the whole sequence instead of escaping each / and . character individually? To get something more readable than
/http:\/\/www\.mydomain\.com\/home/
In the regex parsing site https://regexr.com/ , if I type the url in and set a regex to
/(http ://www.mydomain.com/home)/
, it appears to recognize the string, yet declares an error:
Unescaped forward slash. This may cause issues if copying/pasting this expression into code.
So I'm confused about this issue.
It appears that regex does not offer such a syntax, at least for Javascript. It is possible, however, to proceed as follows:
use a string and automatically escape all the special characters in it,
as indicated here: Javascript regular expression - string to RegEx object
concatenate that string with strings representing the rest of the expression you want to create
transform the string into a regex expression as indicated in Escape string for use in Javascript regex .
I am running up against a weird problem here. When I run:
"#/a/b/c/d".replace("#\/","")
I get what I would expect: a/b/c/d.
But When I precede this regex with a start of string character ^, To get:
"#/a/b/c/d".replace("^#\/","")
This returns the original string "#a/b/c/d".
Could anybody explain why the hash isn't removed and possibly suggest an alternative that would remove the hash only if it appears at the beginning of the string?
The first argument to replace can be either a string or a regular expression.
You are passing a string, so it is looking for an exact match for that string.
Pass a regular expression instead.
"#/a/b/c/d".replace(/^#\//,"")
When you pass a string as the first argument to .replace() method, the string is used as a literal and only 1 replacement can be made.
See the MDN replace reference:
substr (pattern)
A String that is to be replaced by newSubStr. It is treated as a verbatim string and is not interpreted as a regular expression.
What you need is to pass the regex object as the first argument:
"#/a/b/c/d".replace(/^#\//,"")
^^^^^^
There is no point in using a constructor notation (RegExp("^#/") since the pattern is not built dynamically from variables.
There are no double slashes in the pattern! The outer /.../ are called regex delimiters. Then, what is insde is a pattern. The pattern is ^#/, but since the / are used as regex delimiters, the / in the pattern must be escaped to match a literal / (only in the regex literal notation).
String.replace takes as its first argument either a string or a regexp. If you give it a string, it is not interpreted as or converted to a regexp; it is treated literally. Therefore, when you add ^ to your string, it replaces nothing, since there is no ^ in the input.
As explained in the comments, you can either pass a regexp literal (/.../) as the first argument, or you could construct a regexp with new RegExp (but why would you do that?).
I have a JS function which is passed a string that a RegEx is run against, and returns any matches:
searchText= // some string which may or may not contain URLs
Rxp= new RegExp("([a-zA-Z\d]+://)?(\w+:\w+#)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(/.*)?/ig")
return searchText.match(Rxp);
The RegExp should return matches for any of the following (and similar derivations):
google.com
www.google.com
http://www.google.com
http://google.com
google.com?querystring=value
www.google.com?querystring=value
http://www.google.com?querystring=value
http://google.com?querystring=value
However, no such luck. Any suggestions?
In a string, \ has to be escaped: \\.
First, the string is interpreted. \w turns in w, because it has no significant meaning.
Then, the parsed string is turned in a RegEx. But \ is lost during the string parsing, so your RegEx breaks.
Instead of using the RegExp constructor, use RegEx literals:
Rxp = /([a-zA-Z\d]+:\/\/)?(\w+:\w+#)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(\/.*)?/ig;
// Note: I recommend to use a different variable name. Variables starting with a
// capital usually indicate a constructor, by convention.
If you're not 100% sure that the input is a string, it's better to use the exec method, which coerces the argument to a string:
return Rxp.exec(searchText);
Here's a pattern which includes the query string and URL fragment:
/([a-zA-Z\d]+:\/\/)?(\w+:\w+#)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(\/[^?#\s]*)?(\?[^#\s]*)?(#\S*)?/ig
Firstly, there's no real need to create your pattern via the RegExp constructor since it doesn't contain anything dynamic. You can just use the literal /pattern/ instead.
If you do use the constructor, though, you have to remember your pattern is declared as a string, not a literal REGEXP, so you'll need to double-escape special characters, e.g. \\d, not \d. Also, there were several forward slashes you weren't escaping at all.
With the constructor, modifiers (g, i) are passed as a second argument, not appended to the pattern.
So to literally change what you have, it would be:
Rxp= new RegExp("([a-zA-Z\\d]+:\\/\\/)?(\\w+:\\w+#)?([a-zA-Z\\d.-]+\\.[A-Za-z]{2,4})(:\\d+)?(\\/.*)?", "ig")
But better would be:
Rxp = /([a-zA-Z\d]+:\/\/)?(\w+:\w+#)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(\/.*)?/gi;
The following two examples do the same thing.
I was wondering why Option 1 is given in a code example I found and not Option 2?
What is the significance of the forward/backward slashes in '/\&/'
Option 1.
var pairs = qString.split(/\&/);
Option 2.
var pairs = qString.split('&');
split() is a function that can take a regex as well as a string parameter, the forward slash usage is something called a regex literal and it is not really passing a string but a regex object.
The following statements in javascript are the same.
var regex = /\&/; // Literal
var regex = new RegExp("\\&"); // Explicit
Option 1 uses a RegEx constant which is declared with surrounding forward slashed (/).
Option 2 uses a string.
See https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
The first example splits on a regular expression (constructed using the leaning-toothpick (/.../) syntax), while the second splits on a plain string.
Regular expressions are a powerful sub-language that allow complex string matching; in this case, the overhead of using one to split on a literal character (while probably negligible) is a little silly. It's like hiring a top-notch architect to build a wooden cube.
In the first example, the & character is mistakenly escaped (with the \), since it is not special in regular expressions. The regular expression engine gracefully handles that, however, and still treats it as a literal &.
I have the string R.E.M. and I need to make it REM
So far, I have:
$('#request_artist').val().replace(".", "");
...but I get RE.M.
Any ideas?
The first argument to replace() is usually a regular expression.
Use the global modifier:
$('#request_artist').val().replace(/\./g, "");
replace() at MDC
You could pass a regular expression to the replace method and indicate that it should replace all occurrences like this: $('#request_artist').val().replace(/\./g, '');
The method used to replace the string is not recursive, meaning once it found a matching char or string, it stop looking. U should use a regular expression replace. $("#request_artist").val().replace(/\./g, ''); Check out Javascript replace tutorial for more info.