I have the following coce:
if (link.action === "Create") {
However my link.action could be:
Create xxxx
Is there a way I can change this match so it just checks for the start being "Create" ?
Just Check string.indexOf(string_to_check). It returns the index number for a 'string_to_check', if it exists in the string. Any in your case, you want the string start with "Create", so the index should be always 0 in your case.
So, You can try this
if (link.action.indexOf("Create") == 0) {
Use a regular expression.
if (link.action.match(/^Create/) {
}
^ is a special character: an anchor which matches only the beginning of the input.
More reading on regex in general: http://www.regular-expressions.info
link.action.slice(0,6)=="Create"
Will also work as you like as above mentioned methods. For further read String object reference in java script.
Related
I am having an issue with a piece of javascript that's evaluating to true when I do not want it to do so. When indexOf evaluates rec.name - which equals "CSI" - it returns true and triggers the continue line because the finalArr array contains an element named "CSIQ Group". However, that's not what I want, as I only want it to evaluate to true if it finds an element in the array that is an exact match.
Here's the snippet of code:
if(finalArr.join().indexOf(rec.name.toString()) > -1){
continue;
}
What can I change to prevent "CSI" from triggering the continue line when "CSIQ Group" is already in finalArr? Thanks!
Kind Regards,
Joseph
You could try using findIndexOf or in your case just find should do the trick, or even includes.
Something like this:
if(finalArr.includes(rec.name){
continue;
}
includes is great for a simple string match. If you want greater matching then you can try find. find will let you compare each element in the array against a certain condition and you can perform multiple checks on it.
if(!!finalArr.find(element => (element.toLowerCase() === name.rec.toLowerCase()){
continue;
}
I would, however, definitely recommend against converting your array to a string and trying to search it, especially for this case.
You could use RegExp to match the string exactly to CSI:
const pattern = /^CSI$/ // ^ - start of the string, $ - end of the string
pattern.test(rec.name.toString()) // true if only "CSI", false otherwise
You could use this to amend your code and do what you need if word CSI is found.
I'm trying to validate the value of an input text field with the following code:
function onBlurTexto(value) {
var regexNIT = "([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])";
regexCompilado = new RegExp(regexNIT);
if (!(regexCompilado.test(value))) {
alert("Wrong character in text :(");
return false;
} else {
return true;
}
}
But when i enter this text:
!65a
the function returns true (as you can see, the "!" character does not exist in the regular expression)
I'm not an expert in regular expressions, so i think i am missing something in the building of this reg.exp.
How can i put this regular expression to work?
Thanks in advance.
EDIT
i am so sorry ... i should remove the references to the variable "regexpValidar" before posting the issue. I modified the sample. Thanks #TecBrat
You should provide the start (^) and end ($) flags to your regex. Now you are matching 65a since you have alternate sets.
This should work /^([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])+$/g
Demo: https://regex101.com/r/zo2MpN/3
RegExp.test looks for a match in the string, it doesn't verify that the whole string matches the regex. In order to do the latter, you need to add start and end anchors to your regex (i.e. '^' at the start and '$' at the end, so you have "^your regex here$").
I also just noticed that your regex is currently matching only one character. You probably want to add a '+' after the parens so that it matches one or more:
"^([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])+$"
This is wrong. the variable you use doesn't has anything. Try this instead.
var regexCompilado = new RegExp(regexNIT);
Given the following two strings
?room=any_characters123&name=John
?room=any_characters123
I want to extract "any_characters123" using regular expression.
I've tried
(?<=room=)(\w)+(?=\&)
but this one fails on the second string (because the matched string must end with "&").
How can I edit my regular expression so that it matches any_characters123 in both strings?
Since javascript won't support lookbehinds, you need to use capturing group.
\?room=(\w+)
Example:
> var s = "?room=any_characters123&name=John"
> var s1 = "?room=any_characters123"
undefined
> var re = /\?room=(\w+)/;
undefined
> console.log(re.exec(s)[1])
any_characters123
undefined
> console.log(re.exec(s1)[1])
any_characters123
If you're using JS, lookbehind is not supported. You can modify the regex as follows:
room=([^&]+)
Try putting * in the end of your expression:
room=(\w+)\&*?
It will test for zero or plus ocurrences of &
This should do the trick:
/room=(\w+)&?/
/*
find "room=" then match any word characters (at least one) until you
possibly hit "&"
*/
Example:
/room=(\w+)&?/.test("?room=any_characters123")
// => true
"?room=any_characters123".match(/room=(\w+)&?/)
// => ["room=any_characters123", "any_characters123"]
Run the string through two regex tests, the one you already have, and then this one:
(?<=room=)(\w){1,}$
I'm getting nowhere with this...
I need to test a string if it contains %2 and at the same time does not contain /. I can't get it to work using regex. Here is what I have:
var re = new RegExp(/.([^\/]|(%2))*/g);
var s = "somePotentially%2encodedStringwhichMayContain/slashes";
console.log(re.test(s)) // true
Question:
How can I write a regex that checks a string if it contains %2 while not containing any / slashes?
While the link referred to by Sebastian S. is correct, there's an easier way to do this as you only need to check if a single character is not in the string.
/^[^\/]*%2[^\/]*$/
EDIT: Too late... Oh well :P
Try the following:
^(?!.*/).*%2
either use inverse matching as shown here: Regular expression to match a line that doesn't contain a word?
or use indexOf(char) in an if statement. indexOf returns the position of a string or char in a string. If not found, it will return -1:
var s = "test/";
if(s.indexOf("/")!=-1){
//contains "/"
}else {
//doesn't contain "/"
}
The value of product_id might be some combination of letters and numbers, like: GB47NTQQ.
I want to check to see if all but the 3rd and 4th characters are the same.
Something like:
if product_id = GBxxNTQQ //where x could be any number or letter.
//do things
else
//do other things
How can I accomplish this with JavaScript?
Use regular expression and string.match(). Periods are single wildcard characters.
string.match(/GB..NTQQ/);
Use a regular expression match:
if ('GB47NTQQ'.match(/^GB..NTQQ$/)) {
// yes, matches
}
Answers so far have suggested match, but test is likely more appropriate as it returns true or false, whereas match returns null or an array of matches so requires (implicit) type conversion of the result within the condition.
if (/GB..NTQQ/.test(product_id)) {
...
}
if (myString.match(/regex/)) { /*Success!*/ }
You can find more information here: http://www.regular-expressions.info/javascript.html