Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

replaceAll() Function

Featured Replies

I am trying to use the replaceAll() function on some text. If I insert the code as an input variable the code doesn't work. But if I copy the code text right into the script it works just fine. How should I format this so that it works?

 

SHOT-0000.png

 

SHOT-0001.png

you are passing a string with a comma in, the function requires two values

 

try this

import java.util.regex.*

text = 'Test One Two'
code = 'One,Box'
regex = rep.split(',')

result = text.replaceAll(regex[0], regex[1] )
  • Author

much better! thanks! I made it much simpler though...

 

SHOT-0000.png

Eden

The error message gives the clue as to what is expected, and what was given...

You will notice that the 'method' replaceAll can take different types of arguments as well as the simple string, string you are using. But everything you pass to the Groovy comes in as a string and needs to be converted to the required object first.

 

This method would allow you to use a Regex as the first input - but only if you created a regex.Pattern object from the string first, otherwise it would literally look for say 'd+t' in the text rather than digit(s) followed by a tab character.

import java.util.regex.Pattern

pattern = 'd+t'
regex = Pattern.compile(pattern)

The Groovy pages have a great example with closure as the second argument which capitalises each word and makes the rest of it lowercase
 
text.replaceAll('w+',
        { it[0].toUpperCase() + ((it.size() > 1) ? it[1..-1].toLowerCase() : '') }))

This says

for each word (w+), take the first character (it[0]) and Uppercase it, then if the size of the word is more than 1 (it.size() > 1) add the second to the last characters (it[1..-1]) lowercased to it else add nothing (: '')

 

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.