How To Remove Text From A Text Variable
Jump to navigation
Jump to search
Removing text from a Text variable
To remove text from within a Text variable:
- Create > Variables and Questions > Variable(s) > JavaScript Formula(s) > Text.
- In the Expression field, type an expression using the replace method. For example, if your variable was called Vcust1, and it contained commas that you wished to remove, you would write the expression: (Vcust1).replace(/,/g,""). Here, /,/g means that you wish to find all instances of commas. If modifying this expression for other purposes, change the first comma (between the forward slashes) to whatever text you wish to find, and type whatever you wish to replace it with between the two double quotes. See Manipulating Text with JavaScript for more ways of manipulating text.
- Press OK.
Removing text from a text variable when creating a numeric variable
When you change the Question Type from a Text question to Number, Q automatically converts the text to numbers. However, if a non-numeric character is encountered, Q will ignore it and all characters that follow. For example, 123,456,789 will change to 123. This problem is solved by replacing the text as follows:
- Change the Question Type to Number.
- Right-click on the new variable that is created and select Edit Variable.
- You will see an expression such as Q.AsNumeric(Vcust1);, where Vcust1 is the name of the original text variable. Modify this expression by replacing Vcust1 with another expression that replaces the text. For example, if replacing commas, as in the example above, your final expression would be Q.AsNumeric((Vcust1).replace(/,/g,""));.
- Press OK.