Detecting strings that come out of JavaScript

Asked 1 weeks ago, Updated 1 weeks ago, 2 views

Is there a way to detect the string that came out first? For example,

"{1} {2} XXXX"

{1 times} in string as shown aboveWhen {#2} is included, performing different actions according to the number,

When detected by the if statement, "{2} {1} XXXX "Even like this

Run from {#1} first

Is there a way to run the string that came out first?

javascript

2022-09-20 11:07

1 Answers

I'm not sure how complicated the "number one" and "number two" strings are, but if it's not that complicated, you can just pick it as a regular expression. Then it will be possible to obtain in the order found in the string.

const regex = /(foo|dee)/g;
const str = `aaaa dee foo bar`;
let m;

while ((m = regex.exec(str)) !== null) {
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }
  console.log(m[0]);
}


2022-09-20 11:07

If you have any answers or tips


© 2022 pinfo. All rights reserved.