0

I have a string

Apply the function K(i, x<=>k) for more info.

I want to convert K(i, x<=>k) to K_{i}(x<>k), but the arguments inside the function K can vary, so I guess it is something like

string.replace(/\K\([.?],[.?]\)/g, 'K_{$1}($2)')

Edit

Sorry for the vague explanation of what the arguments for K can be. I just thought the regex would be faster if it didn't specify what the arguments could be, but just had some placeholders.

The first argument of K can be only be numbers and letters and the second argument of K can be numbers, letters, spaces, ->, <->, [], <>, &, | and ~, and there can never be more than 2 arguments, so the pattern is always K(something, something).

The remaining string varies as well. So a string could also look like I have a function K(i,a) and two other functions K(3,p&s |r) and K(f, ~g)

7
  • and the problem is...? Commented Oct 10, 2016 at 14:51
  • 1
    Please give more examples if you want a regex that matches multiple cases. Commented Oct 10, 2016 at 14:51
  • 2
    "arguments can vary" ... that's pretty vague to be able to write a regex against Commented Oct 10, 2016 at 14:52
  • is the rest of the string always the same or does that vary too? Commented Oct 10, 2016 at 14:54
  • I have edited my question Commented Oct 10, 2016 at 14:58

1 Answer 1

2

May be something like that (you forgot to group args for the replace value):

string.replace(/K\(([a-zA-Z0-9]+), ?(.+)\)/g, 'K_{$1}($2)')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.