- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2014 10:53 AM
I'm certain that I'm missing something, and equally certain that what I'm missing is small and silly.
In Javascript, you can call string.replace(stringA, stringB), which will replace every instance of stringA with stringB; that's how ServiceNow's string.replace() works. But you can also call string.replace(regExpA, stringB), which will evaluate RegExpA and replace any matches with stringB.
I get into trouble when I try to make that second call w/in ServiceNow, however, and get
Javascript compiler exception: The choice of Java method java.lang.String.replace matching JavaScript argument types (function,string) is ambiguous; candidate methods are: class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence), class java.lang.String replace(char,char)
instead.
I'm sure I probably could do this with the string vs. string replace, but it'd be substantially more verbose at a minimum.
So, my question is: how do I, with a regular expression in hand, do this sort of string replace within ServiceNow?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2014 02:50 PM
Ah-ha!
So, the issue at hand had nothing to do with the javascript string.replace(regex, string) approach: that works perfectly fine... if you're working with a javascript string.
I was not. Unbeknownst to me, I was working with a Java string (despite the fact that it's right there in the error text), which doesn't support that signature.
I corrected the issue by casting it to a string:
var jsString = new String(jString);
and going from there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2014 02:50 PM
Ah-ha!
So, the issue at hand had nothing to do with the javascript string.replace(regex, string) approach: that works perfectly fine... if you're working with a javascript string.
I was not. Unbeknownst to me, I was working with a Java string (despite the fact that it's right there in the error text), which doesn't support that signature.
I corrected the issue by casting it to a string:
var jsString = new String(jString);
and going from there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2014 09:23 AM
You can do that with less code, too:
var jsString = jString + '';
toString() somehow doesn't do the job but the above does.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2014 08:24 AM
Thank you for posting the answer. I just ran into this. If it's not a bug, it's certainly a breach of POLA.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2022 05:00 PM
So... what like 8 years later... I have run into this for the first time... but I have only seen this using Ajax passing a string from client to server and using that string in the .replace. I would almost call this a bug in the ajax code...
Server side the .replace works just fine and you'll find it in many ServiceNow script includes.