0

I'm learning regexs and file saving. As exercise, I wanted to turn every func in a file into function:

data.replace(/func\((.*)\)/g, 'function')

The problem is that the funcs in the file now end up like this:

var thisfunc = function {

}

Instead of this:

var thisfunc = function() {

}

How should I do it so that the regex only replaces the func keyword?

EDIT:

input:

fs.readFile(filename, 'utf8', func(err, data) {
  if (err) throw err
  console.log('OK: ' + filename)
  var newData = data.replace(/func\((.*)\)/g, 'function')
  console.log(newData)
})

var thisfunc = func() {

}
2
  • 2
    what's the input? and btw, did you really learn about file saving in javascript? Commented Feb 1, 2015 at 15:29
  • @Amit Joki updated. See my EDIT. Commented Feb 1, 2015 at 15:30

3 Answers 3

2

match and replace only the opening parenthesis,
especially if those can be nested like in func(a, func(b)):

data.replace(/func\(/g, 'function(')

Note: in environments where replacement string needs escape sequence for (, use function\(, this is not necessary in javascript

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

Comments

1

Your regex is also matching the arguments (in the parenthesis), and the whole match will be replaced by only function. You can either:

  • exclude the arguments from the matching, e.g. with lookahead

    data.replace(/func(?=\((.*)\))/g, 'function')
    
  • or "re"insert them in the replacement string:

    data.replace(/func\((.*)\)/g, 'function($1)')
    

5 Comments

greedy operator is no good here e.g. func() {a} + func() {... and lazy won't help either if parentheses can be nested
@Aprillion: You're right, I've only tackled the concrete problem that the OP had. Of course, regex is in general not the right tool to process non-regular languages.
@Bergi Hey thanks a lot. Could you explain the replacement string one? I can't figure out why the replacement works when put inside the parentheses.
@alexchenco it would replace all the matched characters . In your case the following () is matced.
@alexchenco sorry for previous confusion, it works fine in your case, but it would not work if you had func() ... func() on the same line or if you had the parameters spread on multiple lines like func(a,\n b)
1

Here you actually need a positive lookahead assertion. And you don't need to use capturing group.

data.replace(/func(?=\(.*\))/g, 'function')

OR

.*? here will do a non-greedy match of any character zero or more times.

data.replace(/func(?=\(.*?\))/g, 'function')

(?=\(.*?\)) Asserts that the keyword func must be followed by pair of parenthesis which may or may-not contain arguments.

7 Comments

or data.replace(/func(?=\([^()]*\))/g, 'function')
assuming arguments themselves do not contain parentheses
that's why i added both. but yours will replace all func( to function( even it won't have any closing bracket.
it will replace func( without closing parenthesis, so what? yours will do the same for func(\n{blah()}, just a little bit more slowly
there are many cases where yours and mine would fail. I don't know whats your actual problem is..
|

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.