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.
Related
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)
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
This is my code snippet:
var myString = '#EXTINF:-1 group-title="|FR| CHAINES FRANÇAISES |FR|",|FR|*****CANALSAT*****|FR|';
var group_title = /(group-title=")(\S*)["]/;
var matchgroup_title = group_title.exec(myString);
console.log(matchgroup_title);
I am not familiar to regex so I can't understand this code. Why does exec here return null?
The problem exists because of \S*. If you pop it into regex101.com, you'll see:
So, \s* will match any non-whitespace character, and a space is considered a whitespace character, so this does not match. You can simply use [^"] to check for anything that isn't another quote.
You can simplify this to:
var myString = '#EXTINF:-1 group-title="|FR| CHAINES FRANÇAISES |FR|",|FR|*****CANALSAT*****|FR|';
var group_title = /group-title="([^"]+)"/;
var matchgroup_title = group_title.exec(myString);
console.log(matchgroup_title);
(group-title=") matches group-title="
(\S*) matches zero or more non-whitespace characters, so |FR|
["] then matches a ", but there isn't one (well, there is, but there are whitespace characters first, so no match).
The following helps me to split a string:
var separators = ['\\\.', '\\\(', '\\\)', ':', '\\\?', '\\\!', '\\\"', '\\\n'];
var tokens = someString.split(new RegExp(separators.join('|'), 'g'));
Whenever there is a . the string is split. However, my document sometimes contains ... and when that is the case, I would like to not to split the string three times, but only after the last dot.
To illustrate, consider the following sentences:
(1) I saw the dog. But I didn't care.
(2) The duck didn't see it coming...
(1) should give me an array with two sentences.
(2) should give me an array with one sentence.
As of right now, (2) would give me an array with 3 sentences, as there are three dots.
Is there any way for me to do this easily?
you can use boundaries \b like this /\b\s*\.\s*\b/g
\s* mean zero or more spaces
\. match the . symbol
Regex Demo
Demo Output
const re = /\b\s*\.\s*\b/g;
const str = ` I saw the dog. But I didn't care.`;
const str2 = `The duck didn't see it coming...`;
const str3 = `This is not....the end. this is. not the beginning Linking Park`;
var found = str.split(re);
var found2 = str2.split(re);
var found3 = str3.split(re);
console.log(found);
console.log(found2);
console.log(found3);
for the string with more than two . use /([.]{2,})/g;
const re = /([.]{2,})/g;
const str = `abc...def.ghi`;
var found = str.match(re);
console.log(found);
after that
you can use the following pattern /([^.]+)/g
example in regex101
const re = /([^.]+)/g;
const str = `abc.def.ghi`;
var found = str.match(re);
console.log(found);
The best way to explain this is by example. I'm using jQuery to do this.
Example I have a string
var str = "1.) Ben"
how can I dynamically omit the character 1.) including the space such that str === "Ben"
str can be dynamic such that order can increment from ones, tens, to hundreds.
E.G.
var str = "52.) Ken Bush"
or
var str = "182.) Hailey Quen"
Expected output
str === "Ken Bush"
or
str === "Hailey Quen"
Example
var str = "182.) Hailey Quen"
var test = str.split(') ');
test = test[1];
//output "Hailey Quen"
You can use regex replacement to get what you want.
var str = "182.) Hailey"
var newStr = str.replace(/^\d+\.\)\s*/, '')
// Hailey
var s = "1456.) Hard Spocker".replace(/^\d+\.\)\s*/, '')
// Hard Spocker
^ makes sure that the pattern is matched at the start of the string only
\d+ will match one or more digits.
\. will match the . with escaping
) is a symbol so we need to escape it using \ as \)
\s* will match one or more spaces.
You can learn about these symbols here.
Try using .substring() and .indexOf() as shown :-
var str = "182.) Hailey Quen"
alert(str.substring(str.indexOf(' ')))
DEMO
OR use .split() as shown :-
var str = "182.) Hailey Quen"
alert($.trim(str.split(')')[1]))
DEMO
You can do it regular expression,
var str = "52.) Ken".replace(/\d+\.\)\s/g,"");
console.log(str); //Ken
DEMO
If you have zero or more than zero spaces after the ) symbol then you can use *,
var str = "52.) Ken".replace(/\d+\.\)\s*/g,"");
console.log(str); //Ken
Dismantling regex used,
/ states regex left border
\d d states normal character d, if we want to make it match
numbers then we have to escape it with \
+ It states that one or more number should be there.
\. Again . is a metacharacter to match any valid character, so
escape it.
\) Parenthesis is also a metacharacter to close a group, escape
it.
\s* 12.) can be followed by zero or more spaces.
/ states regex right boundary.
g global flag, which used to do a search recursively.
You can do it like this
var testURL = "182.) Hailey Quen";
var output = testURL.substring(testURL.lastIndexOf(")") + 1).trim();
console.log(output);
*trim function will help to remove extra space if any.Hope it will help