Home > Blockchain >  Is there a way to identify a integer in a string
Is there a way to identify a integer in a string

Time:07-08

I am needing to replace’”’ after an integer in a string. Example “Test” set 12” needs to read “Test” 12 inch.

Replace(Example,’ ”’, ‘ inch’) is giving me “Test inch set 12 inch.

Not all integer are in the same place in the string.

CodePudding user response:

This is the regexp you might be looking for...

/(?<=\d )\"/gm

And with Javascript you could try something like this:

const str = `"Test" set 12"`;

str.replace(/(?<=\d )\"/gm, ' inch');

Output:

"Test" set 12 inch

Explaining the Regexp structure:

/(?<=\d )\"/gm

  • Positive Lookbehind (?<=\d )

    Assert that the Regex below matches (This construct may not be supported in all browsers)

    • \d matches a digit (equivalent to [0-9])
    • matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
  • \" matches the character " with index 3410 (2216 or 428) literally (case sensitive)

  • Global pattern flags

    g modifier: global. All matches (don't return after the first match)

    m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of the string)

  • Related