Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Trying to use regex to find and replace but limited to regexMatch or str_replace

  • cheeseburger
  • cheeseburger's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
2 years 9 months ago #217650 by cheeseburger
Hello all. We are in an Equation type of question where we are running EM to calculate some fields.  The fields from which we get values are each strings and include values with commas in them. For example: 34,999. The following code currently functions perfectly fine by stripping the strings of the commas, converts them to integers, performs the math, and then sets i10L with the value.  

{ i10l = intval(str_replace(",", "", i10d)) - intval(str_replace(",", "", i10i)) - intval(str_replace(",", "", i10j)) - intval(str_replace(",", "", i10k))  }

The above gives us the correct sum and sets i10L correctly.

Prior to sending the sum value to i10L, we need to add commas into the value. In another part of the survey, we've been using a series of regex with JS string replace functions and its been working great in our other scenarios. 

For this current scenario, we found some challenges with the JS and EM not playing well together. We will continue to investigate to help clarify exactly what is happening, but for now it looks like the comma handling seems to need to happen at the EM level. 

Once the calculation completes, we would love to be able to use preg_replace in order to search for a pattern and then insert the commas back into the sum value and then placed back into i10L, but LS only seems to allow str_replace or regexMatch.

str_replace doesnt accept regex, and regexMatch is only a boolean. 

Here's a snippet from our JS showing the pattern that works elsewhere:

.replace(/\B(?=(\d{3})+(?!\d))/g, ",")

Note: We have additional JS handling other things to ensure the values are only numeric and can allow first character negative signs. We can't set the NUMBERS ONLY option in LS for a few reasons. One being that we need the commas and that requires it being a string. 

Any idea how to accomplish this insertion of commas, using just EM in an equation type of question? 
 
Thanks for any help.

 
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose