3

I need to replace 'page.a = b' with 'page.a = node.b'. In a word, i need to turn single word 'b' into 'node.b'; In other word, the string 'page.a = b && page.a < c' shold be transformed into 'page.a = node.b && page.a < node.c', just add 'node.' as the prefix to the single words. Can anyone help me with this? I think the js regexp can work but just cannot find the way with it.

let origin = 'page.a = b'
let expected = 'page.a = node.b';

How do i get the expected string from the origin ?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Lou Kai
  • 31
  • 2
  • 1
    What is the criteria for words that should be replaced? Any word that isn't preceded by `page.`? – Barmar Dec 13 '22 at 03:45
  • Or a word that isn't preceded by `.`? – Barmar Dec 13 '22 at 03:48
  • Either way, you should be able to use a negative lookbehind to match what you want. Also use `\b` word boundaries to match the whole word. – Barmar Dec 13 '22 at 03:48
  • Actually, i want any single word can be replaced with 'node.' prefix. For example, const origin = 'age > 14 && page.num > num && page.name.includes(name)'; I want it to turn it into: 'node.age > 14 && page.num > node.num && page.name.includes(node.name)' ; – Lou Kai Dec 13 '22 at 09:19

2 Answers2

2

You can use negative lookarounds to look for words that aren't preceded by or followed by a dot or a word character:

(?<![.\w])(\w+)(?![.\w])

and replace the matching words with:

node.$1

https://regex101.com/r/GD2N9g/5

blhsing
  • 91,368
  • 6
  • 71
  • 106
1

You can try this approach:

Logic:

  • Split string using equals to(=) to get LHS and RHS as this operation only needs to be done on RHS.
  • Create a regex to match any word + period combination. You can also use non-space characters(/[\s]+/) as this will cover bracket notations and underscore as well
  • For each match, check if they do not have period(.) in it. If not, you can append prefix and return it.
  • Join both halves and return the string

let origin = 'page.a = b'
let o1 = 'page.a = b && page.a < c'

function transformStr(str, prefix) {
  const regex = /([\w.]+)/g;
  const [lhs, rhs] = str.split('=')
  const newRhs = rhs.replace(regex, (part) => {
    return /^\w+$/.test(part) ? `${prefix}.${part}` : part
  })
  return `${lhs}=${newRhs}`
}

console.log(transformStr(origin, 'node'))
console.log(transformStr(o1, 'node'))

As an extension, you can add support for special characters that can exists in a variable. This approach matches non-spaced character groups and processes only the ones that has only word characters or underscore or dollar

function transformStr(str, prefix) {
  const regex = /([^\s]+)/g;
  const [lhs, rhs] = str.split('=')
  const newRhs = rhs.replace(regex, (part) => {
    return /^[\w\$]+$/.test(part) ? `${prefix}.${part}` : part
  })
  return `${lhs}=${newRhs}`
}

console.log(transformStr('page.a = b', 'node'))
console.log(transformStr('page.a = b && page.a < c', 'node'))
console.log(transformStr('page._a = _b && page.a < $c', 'node'))
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Thank you for your attention. Actually, the str can be any javascript expression. It is highly possible that the '=' does not appear. For example, the string is 'test > page.b && page.age < test && page.name.includes(abc)'. The result i want is 'node.test > page.b && page.age < node.test && page.name.includes(node.abc)'. I think regexp may work but just could not find the way with it – Lou Kai Dec 13 '22 at 09:39