indexOf for multiple options - javascript

Let say, I get the following using var content = this.innerHTML:
w here </div>
Using indexOf (or other ways), I want to check for the first position that has either "Space", "<" or "&nbsp".
In this case, it will be 1 (after "w").
What I am confused about is how do I check for the very first position that has either one of these three choices? Do I use Do...while to check for individual "options"?

You're probably looking for a Regular Expression (Regex) and the String#search method. Regex is a bit much to learn all at once, but I'll explain this example code.
You can use square brackets to denote a set of characters, so for example [ <] says "match either a space or a less-than sign."
You can use the pipe | to separate possibilities if you want to match one pattern or another, and that's how to account for matching a non-breaking space HTML entity.
var string = 'w here </div>',
index = string.search(/[ <]| /)
console.log(index) //=> 1

You can use a regular expression with alternations (|), which means "match one of these things". That will also tell you what you found, if that's useful:
function check(str) {
var m = / |<| /.exec(str);
if (!m) {
console.log("Not found in '" + str + "'");
return;
}
console.log("'" + m[0] + "' found at index " + m.index + " in '" + str + "'");
}
check("w here </div>");
check("where </div>");
check("where</div>");

Related

make a regex to validate a string with only one number

here my probleme, in some project i need to validate a string, it must be a length of 4 character and contains one and only one number but i don't know it's place (can be "er1t" or "4frt" but not "sdfd" or "4fd5")
i have made a regex which work : (?:[a-z]\d[a-z]{2}|\d[a-z]{3}|[a-z]{2}\d[a-z]|[a-z]{3}\d) but it's not optimised and I don't find how i search for a number in a string in regex, i use a javascript regex.
Thank in advance,
ekanS
Looks like you can use
^(?=[a-z]*\d[a-z]*$)[a-z\d]{4}$
Demo.
In short, we can set a (?=\D*\d\D*$) condition to check if the whole line/string has only 1 number at the very start of a line/string. Then we can allow any lowercase letters and numbers.
If you plan to allow upper case letters, add A-Z to the character class, or use i option.
function isValid(code) {
var re = /^(?=[a-z]*\d[a-z]*$)[a-z\d]{4}$/gm;
if ((m = re.exec(code)) !== null) {
document.getElementById("res").innerHTML = document.getElementById("res").innerHTML + "<br>" + m[0] + " is <font color=\"#0000FF\">valid</font>.";
}
else {
document.getElementById("res").innerHTML = document.getElementById("res").innerHTML + "<br>" + code + " is <font color=\"#FF0000\">not valid</font>.";
}
}
isValid('er1t');
isValid('4frt');
isValid('sdfd');
isValid('4fd5');
isValid('ffd5');
document.getElementById("res").innerHTML + "</font>"
<div id="res"><font face="Arial"><b>Valid codes:</b><font face="Tahoma"><div>

match word not capitalized a certain way

I want a regular expression that matches all instances of "capitalizedExactlyThisWay" that are not capitalizedExactlyThisWay.
I created a function that finds the indexes of all case insensitive matches and then pushes the values back in like this (JSBIN)
But I would rather just say something like text.replace(regexp,"<highlight>$1</highlight>");
replace has a callback function too.
s = s.replace(reg1, function(m){
if(m===word) return m;
return '<highlight>'+m+'</highlight>';
});
Unfortunately JavaScript regular expressions do not support making only a part of the expression case-insensitive.
You could write a little helper function that does the dirty work:
function capitalizationSensitiveRegex(word) {
var chars = word.split(""), i;
for (i = 0; i < chars.length; i++) {
chars[i] = "[" + chars[i].toLowerCase() + chars[i].toUpperCase() + "]";
}
return new RegExp("(?=\\b" + chars.join("") + "\\b)(?!" + word + ").{" + word.length + "}", "g");
}
Result:
capitalizationSensitiveRegex("capitalizedExactlyThisWay");
=> /(?=\b[cC][aA][pP][iI][tT][aA][lL][iI][zZ][eE][dD][eE][xX][aA][cC][tT][lL][yY][tT][hH][iI][sS][wW][aA][yY]\b)(?!capitalizedExactlyThisWay).{25}/g
Note that this assumes ASCII letters due to limitations of how \b works in JavaScript. It also assumes you're not using any regex meta characters in word (brackets, backslashes, parentheses, stars, dots, etc). An extra step of regex-quoting each char is necessary to make the above stable.
You can use match and map method with a callback:
tok=[], input.match(/\bcapitalizedexactlythisway\b/ig).map( function (m) {
if (m!="capitalizedExactlyThisWay") tok.push(m); });
console.log( tok );
["capitalizedEXACTLYTHISWAY", "capitalizedexactlYthisWay", "capitalizedexactlythisway"]
You could try this regex to match all the case-insensitive exactlythisway string but not of ExactlyThisWay ,
\bcapitalized(?!ExactlyThisWay)(?:[Ee][Xx][Aa][Cc][Tt][Ll][Yy][Tt][Hh][Ii][Ss][Ww][Aa][Yy])\b
Demo
If you could somehow get JavaScript to work with partial case-insensitive matching, i.e. (?i), you could use the following expression:
capitalized(?!ExactlyThisWay)(?i)exactlythisway
If not, you're probably stuck with something like this:
capitalized(?!ExactlyThisWay)[a-zA-Z]+
The downside is that it will also match other variations such as capitalizedfoobar etc.
Demo

check if string is equal to a word and a number

I have an object that contains a string as a property. I want to check that this property is not equal to some word, followed by a space and a number. For instance, something like this:
var TheWordToCheck = "SomeWord";
if (TheObject['SomeProperty'] !== (TheWordToCheck + ' ' + 2)) {...}
if (TheObject['SomeProperty'] !== (TheWordToCheck + ' ' + 3)) {...}
In this example, the code checks for only "SomeWord 2" and "SomeWord 3". How can I simplify this where it checks any numbers?
Thanks.
You could use a regex and the match() method (untested)
var reg = new RegExp("^"+TheWordToCheck+"\\s\\d$")
if (!TheObject['SomeProperty'].match(reg) {...
FIDDLE
depends on the range of numbers you need to check, if it is static or is less than a maximum value, you can use a loop and append the loop variable with the string and check
for (var i=0;i<maxNumber;i++)
{
if (TheObject['SomeProperty'] !== (TheWordToCheck + ' ' + i)) {...
break;
}
}
or you can use regex as suggested in the comments
You can use a regular expression to check this:
var TheWordToCheck = "SomeWord";
var TheObject = {
"SomeProperty": "SomeWord 100"
};
var pattern = new RegExp(TheWordToCheck + ' \\d', 'g');
if (TheObject['SomeProperty'].match(pattern) != null) { ... }
Note that you have to do the backslashes twice in order to make sure that the first one is escaped in the pattern. You should also use the RegEx constructor in order to be able to use a variable in your pattern.

Regular expression for replacing string with javascript

I need help in writing regular expression:
part of my string is fixed and another part of its variable.
only if fixed AND variable string exist i need to alter the string other wise no.
Fixed string:example: AA.BBB.COM
Variable string (may or mayn't exist ): US, but if exist it will be always two letter string with any combination of letter.
In below string if I have variable two letter string exist I want to append “.new”
1 ) https://XY**.US**.AA.BBB.COM
Output: https:// XYZ12**.US.NEW**.AA.BBB.COM
2 ) https://XY.UK.AA.BBB.COM
Output: https:// XYZ12.UK.NEW.AA.BBB.COM
3) https://XY.AA.BBB.COM (no variable string so no change)
Output: https:// XY.AA.BBB.COM
Thanks for your help .
Raghav
Something like the following should get you started, there are other methods. Splitting and parsing might suit better depending on your real requirements:
var s = 'https://XY.US.AA.BBB.COM';
var t = 'https://XY.UK.AA.BBB.COM';
var u = 'https://XY.AA.BBB.COM';
var re = /(\.)(UK|US)(\.)/;
alert(
s.replace(re, '$1' + '$2' + '.NEW' + '$3') + '\n' +
t.replace(re, '$1' + '$2' + '.NEW' + '$3') + '\n' +
u.replace(re, '$1' + '$2' + '.NEW' + '$3')
);

Javascript regular expression - matching multiple occurrences

I'm a little stuck on a problem here.
I'm trying to match multiple occurrences of a regular expression in a string, but I don't get all occurrences:
Sample:
s = new RegExp(';' + y[p][0] + '_' + y[p][1] + '_' + y[p][2] + '_([0-9]*);', 'g');
e = null;
e = s.exec(grArr);
while (e != null) {
alert(e[0]+'-'+e[1]+'-'+e[2]); //debugging output
r = r + e[0]; //adding results to output var
e = s.exec(grArr);
}
Sample variables:
//to be searched:
var grArr=';0_0_709711498101583267971121121179999105110111_11994876;0_0_709711498101583267971121121179999105110111_11994877;0_0_709711498101583267971121121179999105110111_11994878;0_0_709711498101583267971121121179999105110111_11994879;0_0_709711498101583268117110107101108103114252110_11994872;0_0_709711498101583268117110107101108103114252110_11994873;0_0_709711498101583268117110107101108103114252110_11994874;0_0_709711498101583268117110107101108103114252110_11994875;0_0_7097114981015832839910411997114122_11994868;0_0_7097114981015832839910411997114122_11994869;0_0_7097114981015832839910411997114122_11994870;0_0_7097114981015832839910411997114122_11994871;0_1_71114246115115101583276_11994870;0_1_71114246115115101583276_11994874;0_1_71114246115115101583276_11994878;0_1_71114246115115101583277_11994869;0_1_71114246115115101583277_11994873;0_1_71114246115115101583277_11994877;0_1_71114246115115101583283_11994868;0_1_71114246115115101583283_11994872;0_1_71114246115115101583283_11994876;0_1_7111424611511510158328876_11994871;0_1_7111424611511510158328876_11994875;0_1_7111424611511510158328876_11994879;'
//search Pattern:
y[0][0]='0';
y[0][1]='1';
y[0][2]='71114246115115101583283';
This results in 2 occurrences - not 3 as it should be.
The problem is that you're using the semicolon twice: Once at the start of the regex, once at the end.
Since in your example the three "matches" directly follow each other, the second occurrence is not found because its preceding semicolon has already been used in the previous match.
Solution: Use word boundaries ('\\b') instead of ';' in your regex.

Categories

Resources