RegExp test function odd behavior
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm checking whether character is capital letter or not by using javascript RegExp
function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');
at case expected C, O, p, true, true, true
Actual C, O, p, true, false, true
Please help me where i'm doing wrong
javascript
add a comment |
I'm checking whether character is capital letter or not by using javascript RegExp
function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');
at case expected C, O, p, true, true, true
Actual C, O, p, true, false, true
Please help me where i'm doing wrong
javascript
i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.
– Paulquappe
Jan 4 at 13:24
add a comment |
I'm checking whether character is capital letter or not by using javascript RegExp
function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');
at case expected C, O, p, true, true, true
Actual C, O, p, true, false, true
Please help me where i'm doing wrong
javascript
I'm checking whether character is capital letter or not by using javascript RegExp
function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');
at case expected C, O, p, true, true, true
Actual C, O, p, true, false, true
Please help me where i'm doing wrong
javascript
javascript
asked Jan 4 at 13:16
Yaswanth GoliYaswanth Goli
31
31
i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.
– Paulquappe
Jan 4 at 13:24
add a comment |
i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.
– Paulquappe
Jan 4 at 13:24
i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.
– Paulquappe
Jan 4 at 13:24
i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.
– Paulquappe
Jan 4 at 13:24
add a comment |
3 Answers
3
active
oldest
votes
You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.
Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions
add a comment |
This is how you can get array and check every capital letter:
const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));
console.log(res)
+1 for this great solution, but it would be stronger if you useconst res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.
– evayly
Jan 4 at 13:38
@evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.
– Carrm
Jan 7 at 16:09
add a comment |
the below code worked for me hope work for you also. you just have to change your regex like below
function splitWords(text) {
const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039689%2fregexp-test-function-odd-behavior%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.
Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions
add a comment |
You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.
Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions
add a comment |
You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.
Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions
You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.
Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions
answered Jan 4 at 13:44
CarrmCarrm
61121328
61121328
add a comment |
add a comment |
This is how you can get array and check every capital letter:
const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));
console.log(res)
+1 for this great solution, but it would be stronger if you useconst res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.
– evayly
Jan 4 at 13:38
@evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.
– Carrm
Jan 7 at 16:09
add a comment |
This is how you can get array and check every capital letter:
const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));
console.log(res)
+1 for this great solution, but it would be stronger if you useconst res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.
– evayly
Jan 4 at 13:38
@evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.
– Carrm
Jan 7 at 16:09
add a comment |
This is how you can get array and check every capital letter:
const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));
console.log(res)This is how you can get array and check every capital letter:
const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));
console.log(res)const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));
console.log(res)const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));
console.log(res)answered Jan 4 at 13:25
Vadim HulevichVadim Hulevich
846212
846212
+1 for this great solution, but it would be stronger if you useconst res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.
– evayly
Jan 4 at 13:38
@evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.
– Carrm
Jan 7 at 16:09
add a comment |
+1 for this great solution, but it would be stronger if you useconst res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.
– evayly
Jan 4 at 13:38
@evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.
– Carrm
Jan 7 at 16:09
+1 for this great solution, but it would be stronger if you use
const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.– evayly
Jan 4 at 13:38
+1 for this great solution, but it would be stronger if you use
const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.– evayly
Jan 4 at 13:38
@evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.
– Carrm
Jan 7 at 16:09
@evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.
– Carrm
Jan 7 at 16:09
add a comment |
the below code worked for me hope work for you also. you just have to change your regex like below
function splitWords(text) {
const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
add a comment |
the below code worked for me hope work for you also. you just have to change your regex like below
function splitWords(text) {
const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
add a comment |
the below code worked for me hope work for you also. you just have to change your regex like below
function splitWords(text) {
const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
the below code worked for me hope work for you also. you just have to change your regex like below
function splitWords(text) {
const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;
for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
answered Jan 4 at 13:33
jasmin ravaljasmin raval
7210
7210
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039689%2fregexp-test-function-odd-behavior%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.
– Paulquappe
Jan 4 at 13:24