How to get the positions of all Uppercase characters in a string, in jquery ?
assume
var str = "thisIsAString";
answer would be 4,6,7 (with t being at index = 0)
How to get the positions of all Uppercase characters in a string, in jquery ?
assume
var str = "thisIsAString";
answer would be 4,6,7 (with t being at index = 0)
Iterate through the letters and match them to a regex. For example
var inputString = "What have YOU tried?";
var positions = [];
for(var i=0; i<inputString.length; i++){
if(inputString[i].match(/[A-Z]/) != null){
positions.push(i);
}
}
alert(positions);
You'll probably want to use a regular expression to match the appropriate characters (in this case [A-Z]
or similar) and loop over the matches. Something along the lines of:
// Log results to the list
var list = document.getElementById("results");
function log(msg) {
var item = document.createElement("li");
item.innerHTML = msg;
list.appendChild(item);
}
// For an input, match all uppercase characters
var rex = /[A-Z]/g;
var str = "abCdeFghIjkLmnOpqRstUvwXyZ";
var match;
while ((match = rex.exec(str)) !== null) {
log("Found " + match[0] + " at " + match.index);
}
<ol id="results"></ol>
This will loop over the string, matching each uppercase character, and (in the example) adding it to a list. You can just as easily add it to an array or use it as input to a function.
This is the same technique given in the MDN example for RegExp.exec
, and the matches provide some additional data. You can also use more complex regular expressions and this technique should still work.
Simply cam use match()
. Example:
var str = 'thisIsAString';
var matches = str.match(/[A-Z]/g);
console.log(matches);
A simple solution in javascript :)
index = paragraph.match(/[A-Z]/g).map(function (cap) {
return paragraph.indexOf(cap);
});
Hope this helps anyone who comes across this
This should work.
function spinalCase(str) {
let lowercase = str.trim()
let regEx = /\W+|(?=[A-Z])|_/g
let result = lowercase.split(regEx).join("-").toLowerCase()
return result;
}
spinalCase("thisIsAString");
Here's a way
//Your string
const string = "Hello World"
//Splitting string's letters into an array
const stringToArray = string.split("")
//Creating an empty array where you are going to hold your upperCase
indexes
const upperCaseIndexes = []
stringToArray.map((letter,index) => {
//Checking if the letter is capital and not an empty space
if(letter === letter.toUpperCase() && letter != " ") {
//Adding the upperCase's index into the array that hold the indexes
upperCaseIndexes.push(index)
}
})
console.log(upperCaseIndexes)
//You can use the this array to do something with the upperCase letter
//Example
let newString;
upperCaseIndexes.forEach(e => {
stringToArray.splice(e,1, string[e].toLowerCase())
newString = stringToArray.join('')
})
Pseudocode (don't have much time)
var locations = [];
function getUpperLocs(var text = ''){
for(var i = 0;i == check.length();i++)
{
if(checkUpper(text){
locations.push(i)
}
}
function checkUppercase(var str = '') {
return str === str.toUpper();
}
}
allright no to much pseudocode but probably some syntax errors..