Find a number of occurrences of character in string

ilyo

I have a string and I want to remove the first 3 occurrences of the letter a (for example).

Seems regex has no solution for a specific number of occurrences. And I can't use replace without loops, which I want to avoid.

Jongware

Nice challenge.

This works:

var str = "I have a string and I want to remove the first 3 occurrences of the letter a (for example).";
str = str.replace(/^([^a]*a){3}/, function(match) {
  return match.replace(/a/g, '!');
});
document.body.innerHTML = str;

Result:

I h!ve ! string !nd I want to remove the first 3 occurrences of the letter a (for example).

The first replace matches a continuous string containing exactly 3 as. Its inner replace changes the a to a ! for clarity; you can safely replace it with nothing as well.

This is scaleable to any number because you can change the {3} to the required number of changes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Count the number occurrences of a character in a string

From Java

Count the number of occurrences of a character in a string in Javascript

From Java

PostgreSQL SQL query to find number of occurrences of substring in string

From Dev

Sort Array based on number of character occurrences

From Dev

Find and count the number of occurrences in a sheet

From Dev

VBA: Count number of occurrences of the most frequent character in a string

From Dev

Regex to limit the number of occurrences of a particular character in a string

From Dev

Recursively counting character occurrences in a string

From Dev

Ruby:-Find the occurrences of a character in a file

From Dev

How do I count the number of occurrences of a character in a string?

From Dev

elasticsearch how to find number of occurrences

From Dev

Using innerHTML to detect the number of occurrences of a character?

From Dev

Find number of occurrences of a particular string within a group of characters WITH A CATCH

From Dev

R: find if number is within range in a character string

From Dev

Find total number of occurrences of a substring

From Dev

how to Count the number of occurrences of a character in a string but not one of them?

From Dev

regex to find string and next character should not be a number

From Dev

How can I count the number of occurrences in a string based on character letter?

From Dev

to find Second last value of a string in number or character

From Dev

Find and count the number of occurrences in a sheet

From Dev

How to find the total number of occurrences of the characters of a word in a string?

From Dev

Find occurrences of a word in a string

From Dev

Average of occurrences of a character in a string array

From Dev

Count number of string occurrences

From Dev

Find a number of occurrences of character in string

From Dev

Find-Replace Multiple Occurrences of a string and append iterating number

From Dev

how to find number of occurrences in ML string list?

From Dev

Count the number of occurrences of a character in a string for a number of queries?

From Dev

Find the number of occurrences of strings separated by comma and output the string

Related Related

  1. 1

    Count the number occurrences of a character in a string

  2. 2

    Count the number of occurrences of a character in a string in Javascript

  3. 3

    PostgreSQL SQL query to find number of occurrences of substring in string

  4. 4

    Sort Array based on number of character occurrences

  5. 5

    Find and count the number of occurrences in a sheet

  6. 6

    VBA: Count number of occurrences of the most frequent character in a string

  7. 7

    Regex to limit the number of occurrences of a particular character in a string

  8. 8

    Recursively counting character occurrences in a string

  9. 9

    Ruby:-Find the occurrences of a character in a file

  10. 10

    How do I count the number of occurrences of a character in a string?

  11. 11

    elasticsearch how to find number of occurrences

  12. 12

    Using innerHTML to detect the number of occurrences of a character?

  13. 13

    Find number of occurrences of a particular string within a group of characters WITH A CATCH

  14. 14

    R: find if number is within range in a character string

  15. 15

    Find total number of occurrences of a substring

  16. 16

    how to Count the number of occurrences of a character in a string but not one of them?

  17. 17

    regex to find string and next character should not be a number

  18. 18

    How can I count the number of occurrences in a string based on character letter?

  19. 19

    to find Second last value of a string in number or character

  20. 20

    Find and count the number of occurrences in a sheet

  21. 21

    How to find the total number of occurrences of the characters of a word in a string?

  22. 22

    Find occurrences of a word in a string

  23. 23

    Average of occurrences of a character in a string array

  24. 24

    Count number of string occurrences

  25. 25

    Find a number of occurrences of character in string

  26. 26

    Find-Replace Multiple Occurrences of a string and append iterating number

  27. 27

    how to find number of occurrences in ML string list?

  28. 28

    Count the number of occurrences of a character in a string for a number of queries?

  29. 29

    Find the number of occurrences of strings separated by comma and output the string

HotTag

Archive