JavaScript Find second occurrence of a character in a string find second occurrence of character in string python,find second

I have heard that JavaScript has a function called search() that can search for a string ( lets call it A ) in another string ( B ) and it will return the first position at which A was found in B.

var str = "Ana has apples!";
var n = str.search(" ");

The code should return 3 as its the first position in which the space was found in str.

And I was wondering if there is a function that can find the next spaces in my string.

For example I want to find the length of the first word in my string and I could easily do this If I knew the its starting position and its ending one.

If there is such a function, are there any better than it for such things?

Answer:1

You need to use String.indexOf method. It accepts the following arguments:

str.indexOf(searchValue[, fromIndex])

So you can do this:

var str = "Ana has apples!";
var pos1 = str.indexOf(" ");           // 3
var pos2 = str.indexOf(" ", pos1 + 1); // 7
console.log(pos2 - pos1 - 1);          // 3... length of the second word
Answer:2

Better for matching is to use regex. There is option like match group using group 'g' flag

var str = "Ana has apples  !";
var regBuilder = new RegExp(" ","ig");
var matched = "";
while((matched = regBuilder.exec(str))){
    console.log(matched + ", position : " +matched.index);
}

str = "Ana is Ana no one is better than Ana";
regBuilder = new RegExp("Ana","ig");
while((matched = regBuilder.exec(str))){
    console.log(matched + ", position : " +matched.index);
}
Answer:3

Try this:

const str = "Ana has apples!";

const spaces = str.split('')
                  .map((c, i) => (c === ' ') ? i : -1)
                  .filter((c) => c !== -1);

console.log(spaces);
Answer:4

I have an an array with key-value pair, array columns are id and name. I want to sort this array by id. The id column value is of type string type but I want to sort them as numeric values. var ...

I have an an array with key-value pair, array columns are id and name. I want to sort this array by id. The id column value is of type string type but I want to sort them as numeric values. var ...

  1. sort array objects javascript
  2. sort array objects alphabetically javascript
  3. sort array objects by property
  4. sort array objects php
  5. sort array objects js
  6. sort array objects by property javascript
  7. sort array objects typescript
  8. sort array objects
  9. sort array objects java
  10. sort array objects swift
  11. sort array objects c#
  12. sort array objects by property js
  13. sort array objects by date
  14. sort array objects by value
  15. sort array objects jquery
  16. sort array objects twig
  17. sort array objects ios
  18. sort objects array ruby
  19. sort array of objects by key javascript
  20. sort array of objects by key value

I'm using Django and Python 3. I have this in my HTML code: {% for category in categories() %} <li class="c-menu__item fs-xsmall"> <a href="#" id="next-category"> ...

I'm using Django and Python 3. I have this in my HTML code: {% for category in categories() %} <li class="c-menu__item fs-xsmall"> <a href="#" id="next-category"> ...

I'm trying to use the panResonder in my React Native app. I tried doing so using class properties instead of constructor and super(). Here is the code: export default class Deck extends Component { ...

I'm trying to use the panResonder in my React Native app. I tried doing so using class properties instead of constructor and super(). Here is the code: export default class Deck extends Component { ...

  1. es6 react native
  2. es6 syntax react native
  3. es6-promise react-native
  4. es6 features in react native
  5. react native es6 support
  6. react native es6 tutorial
  7. react native es6 function
  8. react native es6 classes
  9. react native es6 modules
  10. react-native es6-symbol
  11. es6 trong react native

I want to customize the search displayed by Global Search Box in Dynamics Crm by applying some custom Javascript to it. Is there any way to achieve it

I want to customize the search displayed by Global Search Box in Dynamics Crm by applying some custom Javascript to it. Is there any way to achieve it

  1. customize global search salesforce
  2. customize global search salesforce lightning
  3. customize global search
  4. custom global search lightning component
  5. salesforce customize global search results
  6. servicenow customize global search
  7. custom object global search
  8. can we customize global search in salesforce
  9. dynamics 365 customize global search
  10. how do i customize global search in salesforce
  11. customize global search in salesforce