This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
javascript includes() case insensitive
(11 answers)
Closed 2 years ago.
I'm using the following code to search sub string in a string
mystring.search(new RegExp(substring, 'i'))
Reason why I am using new RegExp is, I want to search case insensitive. However, when there is a string like
var mystring = '10" stick';
and I want to search 10", the code above does not return any result. It's clearly because of new RegExp and double quote. Is there any particular flag that needs to be passed in new RegExp? I googled a lot but couldn't find any solution. What am I missing?
search returns match position, maybe ur confused by this.
So, try this out,
const myString = '10" stick';
console.log(myString.match(new RegExp('10"', 'i'))[0])
console.log(myString.match(new RegExp('ick', 'i'))[0])
console.log(myString.match(new RegExp('asd"', 'i'))) // non-matching
It returns the match or null if theres non. AND it matches 10" in 10" stick
see String.prototype.search mdn
Related
This question already has answers here:
Getting content between curly braces in JavaScript with regex
(5 answers)
Closed 2 years ago.
I need to match a specific regex syntax and split them so that we can match them to an equivalent value from a dictionary.
Input:
{Expr "string"}
{Expr "string"}{Expr}
Current code:
value.match(/\{.*\}$/g)
Desired Output:
[{Expr "string"}]
[{Expr "string"},{Expr}]
Use a non-greedy quantifier .*?. And don't use $, because that forces it to match all the way to the end of the string.
value = '{Expr "string"}{Expr}'
console.log(value.match(/\{.*?\}/g));
One option, assuming your version of JavaScript support it, would be to split the input on the following regex pattern:
(?<=\})(?=\{)
This says to split at each }{ junction between two terms.
var input = "{Expr \"string\"}{Expr}";
var parts = input.split(/(?<=\})(?=\{)/);
console.log(parts);
This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 2 years ago.
String example: "{something}" or "{word: something}"
What I need to do is get 'something', so the text between two specific parts of the string, in these case {-} and {word:-} (The 'something' part can change in every string, I never know what it is).
I tried using string.find() or regex but I didn't come up with a conclusion. What's the quickest and best way to do this?
What you need is a capture group inside a regex.
> const match = /\{([^}]+)\}/.exec('{foo}')
> match[1]
'foo'
The stuff in the parens, ([^}]+), matches any character but }, repeated at least once. The parens make it be captured; the first captured group is indexed as match[1].
This question already has answers here:
How to detect exact length in regex
(8 answers)
Closed 4 years ago.
So I am trying to use a regular expression to check against strings but it doesn't seem to be working properly.
Basically I want it to match a alpha-numeric string that is exactly 3 characters long. The expression I am using below does not seem to be working for this:
const msg = message.content;
const regex = /[A-Za-z0-9]{3}/g;
if (msg.match(regex)) {
// Do something
}
Am I doing something wrong? Any help would be appreciated. Thanks in advance.
You need to add ^ and $ for the start-of-string anchor and end-of-string anchor, respectively - otherwise, for example, for #123, the 123 will match, and it will pass the regex. You also might consider using the i flag rather than repeat A-Za-z, and you can use \d instead of 0-9.
It looks like you just want to check whether the string passes the regex's test or not, in which case .test (evaluates to a boolean) might be a bit more appropriate than .match. Also, either way, there's no need for the global flag if you're just checking whether a string passes a regex:
const regex = /^[a-z\d]{3}$/i;
if (regex.test(msg)) {
// do something
}
This question already has answers here:
Replace forward slash "/ " character in JavaScript string?
(9 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -
var test= '01/01/1986';
test.replace('//g','-')
but it does't give desire result. Any pointer would be helpful.
You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:
var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
A quick way is to use split and join.
var test= '01/01/1986';
var result = test.split('/').join('-');
console.log(result);
Note too that you need to save the result. The original string itself will never be modified.
This question already has answers here:
JavaScript RegExp objects
(5 answers)
Closed 6 years ago.
I am trying to validate if a string I entered matches the date format 'MM/yyyy'
Below is a sample of the code I am using for the same:
var date='05/2016'
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$');
patt.test(date);
However the above code is returning false.
I tried running it with the regex checker:
https://regex101.com/
The pattern seems to be working fine.
Could someone please let me know what is missing.
https://jsfiddle.net/ymj6o8La/
You have to escape the string that is passed to RegExp (the backslashes).
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
Even better, in your case, it's not dynamic, so you should use the literal RegExp instead
var patt = /^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$/
You should escape your backslashes. To represent \d or even \ you should another backslash behind it (e.g: \\) :
var date = '05/2016'
var patt = new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
console.log(patt.test(date));
Try using a pattern like this
patt= /^((0[1-9])|(1[0-2]))\/(\d{4})$/;