1

I am using node.js and express.

I have a string similar to the following:

Here is my string but now I need to processString("mystring") 
and return the processString("data").

I want to search for all occurrences of processString and replace it with the results of a function. The function I need to run is asynchronous. Is there an easy way to do this?

Thanks!

1
  • Okay, so this is what I'm thinking I should do. Let me know if you think this is the right way or any better: 1. Do a regex match on processString 2. async.forEach over results 3. Do a string replace the regex match with the results from processString Does this seem right? Commented Nov 9, 2012 at 23:27

1 Answer 1

1

Your question isn't really to clear, the "why" and "what" may be more helpfull in answering your question. From what I understand you want to replace processString("****") with **** in which case you can use the following:

    var str = 'Here is my string but now I need to processString("mystring") and return the processString("data").'
    str = str.replace(/processString\(\".*?\"\)/gi, function(match){
         return match.replace("processString\(\"", "").replace("\"\)", "")
    })
    console.log(str);

This will match the processString("****") inner contents, hope this is what you were looking for!

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

3 Comments

Sorry, I tried to figure out how to make it as clear as possible, the parameter of the method needs to be processed by a function (processString) and it queries a database. As the argument is from the user and can be anything, I can't avoid the database call. So, the function that gets called has to be asynchronous. Does that make sense?
I think I understand the question, however the source would help. With the understanding I have you can use case .replace() which is non blocking, so you can pass whatever paramaters you need through the callback, I think this would be the best way to iterate over the items in the string. You could also avoid multiple queries by building a query index (collect all parameters), doing one query, and then replacing each result accordingly.
I don't have any code for it yet as I'm trying to figure out the best way for it to work. If I use replace, is there a way to know when it is done so I can call the function's callback?

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.