3

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)

user123456789
  • 41
  • 1
  • 1
  • 3

7 Answers7

13

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);
Dropout
  • 13,653
  • 10
  • 56
  • 109
  • 1
    Since you're matching against single characters, I don't think you need the `g` flag on your RegExp. You can probably make this faster (for large strings) by assigning the regex to a variable once outside of the loop. – ssube Nov 26 '14 at 15:39
  • Fixed the greedy quantifier. – Dropout Nov 27 '14 at 07:39
1

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.

ssube
  • 47,010
  • 7
  • 103
  • 140
1

Simply cam use match(). Example:

var str = 'thisIsAString';
var matches = str.match(/[A-Z]/g);
console.log(matches);
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

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

bsewk
  • 1
0

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");
0

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('')
  })
Panos
  • 1
  • 1
-1

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..

Gerton
  • 676
  • 3
  • 14