简体   繁体   中英

React js regex find all words beginning and ending with __

I have a txt file like this:

Io sottoscritto/a __NOME__
nato a __CITTA_NASCITA__(__SIGLA_CITTA_NASCITA__) il __DATA_NASCITA__
residente a __RESIDENZA__   in via __VIA_RESIDENZA__    n __NUMERO_RESIDENZA__.

I have to make sure that when user loads the file in question, an array is created containing all the words starting with __ and ending with __, of the type __NAME__

I managed to do this the problem that the regex also finds me the following words together: ["__CITTA_NASCITA__(__SIGLA_CITTA_NASCITA__)", "__NUMERO_RESIDENZA__."]

In the first case the two words must be separated.

In the second case there must be no end point.

Result:

["__CITTA_NASCITA__", "__SIGLA_CITTA_NASCITA__", "__NUMERO_RESIDENZA__"]

Can you tell me where I'm wrong?

link: https://codesandbox.io/s/lively-butterfly-ey8og?file=/src/App.js

export default function App() {
  const showFile = async (e) => {
    e.preventDefault();
    const reader = new FileReader();
    reader.onload = async (e) => {
      const text = e.target.result;
      const row = text.split("\n");
      let array = [];
      row.map((el) =>
        el.split(" ").map((ul) => {
          var reg = /^__[A-Za-z]*__/;
          if (reg.test(ul)) {
            console.log(ul);
            array.push(ul);
          }
        })
      );
      console.log(array);
    };
    reader.readAsText(e.target.files[0]);
  };

  return (
    <div className="App">
      <input type="file" onChange={(e) => showFile(e)} />
    </div>
  );
}

You may fix the code by extracting all matches directly from the text variable:

 const reg = /__[A-Z_]+__/ig;

  const showFile = async (e) => {
    e.preventDefault();
    const reader = new FileReader();
    reader.onload = async (e) => {
      const text = e.target.result;
      let array = text.match(reg);
      setArray(array);
    };
    reader.readAsText(e.target.files[0]);
  };

Note if you only allow a single underscore between __ delimiters, you will need to use the

const reg = /__[A-Z]+(?:_[A-Z]+)*__/ig;

Also, see the row and row.map(...) code parts are unnecessary here.

I see the code and run the example on sandbox I changed the regex a little to " [A-Za-z_]+? " this it is more accurate now and works accordingly the way you wanted.

![output] enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM