match a path for a specific word and a / and any characters that follow.
For example.
const str = 'cars/ford';
const isCars = str.match('cars');
What I want to do is make sure it matches cars and has a slash and characters after the / then return true or false.
The characters after cars/... will change so I can't match it excatly. Just need to match any characters along with the /
Would love to use regex not sure what it should be. Looking into how to achieve that via regex tutorials.
var str = "cars/ford";
var patt = new RegExp("^cars/"); //or var patt = /^cars\//
var res = patt.test(str); //true
console.log(res);
https://www.w3schools.com/js/js_regexp.asp
https://www.rexegg.com/regex-quickstart.html
You could use test() that returns true or false.
const str = "cars/ford";
const str2 = "cars/";
var isCars = (str)=>/^cars\/./i.test(str)
console.log(isCars(str));
console.log(isCars(str2));
Here is a quick regex to match "cars/" followed by any characters a-z.
(cars\/[a-z]+)
This will only match lowercase letters, so you can add the i flag to make it case insensitive.
/(cars\/[a-z]+)/i
It is a basic regular expression
var str = "cars/ford"
var result = str.match(/^cars\/(.*)$/)
console.log(result)
^ - start
cars - match exact characters
\/ - match /, the \ escapes it
(.*) - capture group, match anything
$ - end of line
Visualize it: RegExper
Related
So I have this (example) string: 1234VAR239582358X
And I want to get what's in between VAR and X. I can easily replace it using .replace(/VAR.*X/, "replacement");
But, how would I get the /VAR.*X/as a variable?
I think what you are looking for might be
string.match(/VAR(.*)X/)[1]
The brackets around the .* mark a group. Those groups are returned inside the Array that match creates :)
If you want to only replace what's in between "VAR" and "X" it would be
string.replace(/VAR(.*)X/, "VAR" + "replacement" + "X");
Or more generic:
string.replace(/(VAR).*(X)/, "$1replacement$2");
You can try use the RegExp class, new RegExp(`${VAR}.*X`)
You can store it as variable like this,
const pattern = "VAR.*X";
const reg = new RegExp(pattern);
Then use,
.replace(reg, "replacement");
If you
want to get what's in between VAR and X
then using .* would do the job for the given example string.
But note that is will match until the end of the string, and then backtrack to the first occurrence of X it can match, being the last occurrence of the X char in the string and possible match too much.
If you want to match only the digits, you can match 1+ digits in a capture group using VAR(\d+)X
const regex = /VAR(\d+)X/;
const str = "1234VAR239582358X";
const m = str.match(regex);
if (m) {
let myVariable = m[1];
console.log(myVariable);
}
Or you can match until the first occurrence of an X char using a negated character class VAR([^\r\nX]+)X
const regex = /VAR([^\r\nX]+)X/;
const str = "1234VAR239582358X";
const m = str.match(regex);
if (m) {
let myVariable = m[1];
console.log(myVariable);
}
I have a string which looks like below
str = "hey there = pola"
Now I need to check if there is equal = sign and the first word to the left of it. So this is what I do
str.match(/\w+(?= *=)/)[0]
So I get the desired result
But say I have a string like this
str = "hey there= pola so = boba"
Now I have two = signs. But the above regex will only give me the result for the first = sign.
Is there any regex that can always look for the first instance of = from the end of the string?
You can assert what is on the right is an equals sign followed by matching any char except an equals sign until the end of the string
\w+(?= *=[^=]*$)
In parts:
\w+
(?= Positive lookahead
*= Match 0+ occurrences of a space followed by =
[^=]* Match 0+ occurrences of = ( Use [^=\r\n]* to not cross line breaks)
$ End of string
) Close lookahead
Regex demo
const regex = /\w+(?= *=[^=]*$)/;
const str = `hey there= pola so = boba`;
console.log(str.match(regex)[0]);
Without using a lookahead, you could use a capturing group:
^.*\b(\w+) *=[^=]*$
Regex demo
const regex = /^.*\b(\w+) *=[^=]*$/m;
const str = `hey there= pola so = boba`;
console.log(str.match(regex)[1]);
I'm not much expert on regex but for you requirement I think split and pop should work
let str = "hey there= pola so = boba";
let endres = str.split('=').pop(); // gives the last element in the split array
Hope this helps.
I want to replace a character but nothing happens.
const str = '//id//user/param//test';
const result = str.replace(/[//]/gi, '/');
This is what i get :
//id//user/param//test
This is what i want :
/id/user/param/test
[...] denotes a character group, which matches any one of these characters. So, [//] essentially means "match / or /". Thus [//] is the same as [/].
You don't want a character group:
const str = '//id//user/param//test';
console.log(str.replace(/\/\//gi, '/'));
If you want to match two or more /, use the + or {2,} quantifiers:
/\/{2,}/
/\/\/+/
You can do it also using a regex group /\/+/
const str = '//id//user/param//test';
const result = str.replace(/\/+/g, '/')
console.log(result)
I need a regular expression in Javascript for Indian vehicle NUMBER.
Expression should match following patterns.
GL/48/ED/1220
MH/24/ L/3654
I have tried following pattern but didn't work.
str = "MH/14/AA/2000";
var pattern = /[A-Za-z][A-Za-z]/[0-9][0-9]/[A-Za-z ][a-z]/[0-9][0-9][0-9][0-9]$/;
var result = str.match(pattern);
console.log(result);
result gives null.
Anyone have solution for it?
There are three problems
Un-escaped /
/ will end your regular expression, so you need to escape those which are in the middle of it
var pattern = /[A-Za-z][A-Za-z]\/[0-9][0-9]\/[A-Za-z ][a-z]\/[0-9][0-9][0-9][0-9]$/;
typo - pattern instead of patt1
i.e.
var result = str.match(pattern);
case-sensitive matching
Either use [A-Z] with i to ignore case-sensitive matching or just use [A-Z] in all of them
Finally
str = "MH/14/AA/2000";
var pattern = /[A-Z][A-Z]\/[0-9][0-9]\/[A-Z][A-Z]\/[0-9][0-9][0-9][0-9]$/i;
var result = str.match(pattern);
console.log(result);
Less verbose version
str = "MH/14/AA/2000";
var pattern = /[A-Z]{2}\/[0-9]{2}\/[A-Z]{2}\/\d{4}$/i;
var result = str.match(pattern);
console.log(result);
var pattern = /[A-Za-z][A-Za-z]/[0-9][0-9]/[A-Za-z ][A-Za-z]/[0-9][0-9][0-9][0-9]$/;
The diff to your expression is:
The slashes / need to be escaped with backslash \, the second part of letters missed the uppercase A-Z
Need to replace a substring in URL (technically just a string) with javascript.
The string like
http://blah-blah.com/search?par_one=test&par_two=anothertest&SearchableText=TO_REPLACE
or
http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest
means, the word to replace can be either at the most end of the URL or in the middle of it.
I am trying to cover these with the following:
var newWord = NEW_SEARCH_TERM;
var str = 'http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest';
var regex = /^\S+SearchableText=(.*)&?\S*$/;
str = str.replace(regex, newWord);
But no matter what I do I get str = NEW_SEARCH_TERM. Moreover the regular expression when I try it in RegExhibit, selects the word to replace and everything that follows it that is not what I want.
How can I write a universal expression to cover both cases and make the correct string be saved in the variable?
str.replace(/SearchableText=[^&]*/, 'SearchableText=' + newWord)
The \S+ and \S* in your regex match all non-whitespace characters.
You probably want to remove them and the anchors.
http://jsfiddle.net/mplungjan/ZGbsY/
ClyFish did it while I was fiddling
var url1="http://blah-blah.com/search?par_one=test&par_two=anothertest&SearchableText=TO_REPLACE";
var url2 ="http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest"
var newWord = "foo";
function replaceSearch(str,newWord) {
var regex = /SearchableText=[^&]*/;
return str.replace(regex, "SearchableText="+newWord);
}
document.write(replaceSearch(url1,newWord))
document.write('<hr>');
document.write(replaceSearch(url2,newWord))