Pick singularly characters in a multiple string

Asked

Viewed 131 times

5

Hello, I need help with a regular expression that satisfies some occurrences of a text file.

In this case, I need a regular expression that finds occurrences where there are a minimum number of characters in a pattern. For example:

I have the following string: "'C'; 'AEBDCEAB'; 'A'; 'B'"...

In case, I want to get only the characters of "AEBDEAB", so that I can use each one (in the case, the grouping is for each character, and not the whole group, as in a /[A-E]/).

  • 3

    I could not understand what you want. Try to put an example and, as in the case above, because one satisfies and the other does not. Perhaps the minimum definition is missing.

2 answers

3

William, try it like this:

var str = "'C'; 'AEBDCEAB'; 'A'; 'B'";
var resultado = str.match("[a-zA-Z]{8}");

I used the function match.

This will give an array where you can get what you want using resultado[0]. If you want each letter of this group you can use resultado[0].split('')

Example


In case this group appears several times you can use:

var str = "'C'; 'AEBDCEAB'; 'A'; 'B' 'AEBFCEAB' 'AEBXCEAB'";
var resultado = str.match(/[a-zA-Z]{8}/g);
console.log(resultado); // dá ["AEBDCEAB", "AEBFCEAB", "AEBXCEAB"] 

Example

  • 1

    Wouldn’t it be better to use: var resultado = str.match("[a-zA-Z]{8}"); to get only the correct value in the array?

  • @Pauloroberto, give your own answer or edit this one. I put it like this in case there are more occurrences in the string, otherwise it only gives the first.

  • Got it @Sergio. In this case we should make available the two options, :D

  • 1

    @Pauloroberto, I’ve already edited.

  • 2

    The author of the question said that he wants a "regular expression that finds occurrences where there is a number minimum of characters in a pattern". In this case instead of {8}, use {8,} (or {n,}, where n is the minimum)

  • @Nulluserexception, truth, add this to the answer...

  • 1

    I don’t know if there was a typo but @Guilherme despises the letter C of the desired result. Coincidentally the character 'C' is the initial. I believe a more detailed explanation of what he wants is needed.

  • Is there any difference between using match("[a-z]") against match(/[a-z]/)? Or is it just another case of weak js typing?

  • 1

    @Guilhermebernal, I can’t answer, but on MDN it says A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

Show 4 more comments

0

This regular expression is a minimum of 8 characters, a-za-Z characters:

[[:alpha:]]{8,}

or

[a-zA-Z]{8,}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.