Skip to content
Snippets Groups Projects
Commit f28df466 authored by Johannes Goderbauer's avatar Johannes Goderbauer
Browse files

[Projekt: Entwicklung - Neon][TicketNr.: 1060894][AttributeFilter funktionieren nicht mehr]

fixed wrong replace with "g" modifier
parent 9998632b
No related branches found
No related tags found
No related merge requests found
......@@ -222,7 +222,7 @@ AttributeFilterExtensionMaker.getFilterCondition = function(pObjectType, pFilter
condition = newWhere(["AB_ATTRIBUTERELATION", dbField], SqlUtils.escapeVars(pRawValue), pOperatorName == "=" ? SqlBuilder.EQUAL() : SqlBuilder.NOT_EQUAL());
else
{
condition = pCondition.replace(new RegExp(pColumnPlaceholder, "g"), dbField);
condition = StringUtils.replaceAll(pCondition, pColumnPlaceholder, dbField);
}
//a SqlBuilder.IN() in a newWhere is not used here since the subselect can contain a placeholder
......
......@@ -15,6 +15,34 @@ import("system.datetime");
import("Offer_lib");
import("Date_lib");
/**
* Class containing static utility functions for regular expression objects (RegExp)
* Do not create an instance of this
*
* @class
*/
function RegExpUtils(){}
/**
* Escapes characters of a string that shall be passed to a RegExp 1:1 <br/>
* This is necessary because the RegExp-constructor uses the given string directly as pattern as you can see here:<br/>
* new RegExp("^t.*t$") <=> /^t.*t$/<br/>
* while<br/>
* new RegExp(escapeStr("^t.*t$")) <=> /\^t\.\*t\$/<br/>
* The first will match any string taht starts with "t" and ends with "t" like the word "test". <br/>
* The second one will match only the string "^t.*t$".<br/>
* You have to decide what you want to use as a RegExp and what is correct for your case.<br/>
*
* @param {String} pStringForRegExp <p/>The plain string that has to be escaped to be used as an RegExp
*
* @return {String} the escaped string
*/
RegExpUtils.escapePatternStr = function (pStringForRegExp)
{
//source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
return pStringForRegExp.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
};
/**
* Class containing static utility functions for string-actions
* Do not create an instance of this
......@@ -23,6 +51,56 @@ import("Date_lib");
*/
function StringUtils(){}
/**
* Searches and replaces all matches of a string within another string with a given replacement.
* This will not modifiy the original string but return a new string with the applied replacements.
*
* @param {String} pPlainInputStr <p/>String where the matches of the searched word are replaced
* @param {String} pPlainSearchStr <p/>Value that is replaced in the pPlainInputStr; this has to be a plain string
* @param {String|function} pReplacement <p/>A string or a function for the replacement-value just like the String.prototype.replace(...) accepts
* @param {String} [pFlags="g"] <p/>If specified, flags is a string that contains the modifier-flags to add
* (for exmaple "i", "m", "im", etc.;
* <p/> for more details take a look at the constructor of the standard-javascript RegExp the "g" modifier
* is added automatically, so there is no need to set this modifier
*
* @return {String} <p/>A new string, with all matches of the given search string replaced by a replacement
*
* @example
*
* var is = "an apple is an apple is an APPLE";
*
* var r1 = StringUtils.replaceAll(is, "apple", "orange");
* var r2 = StringUtils.replaceAll(is, "apple", "orange", "g");
* var r3 = StringUtils.replaceAll(is, "apple", "orange", "i");
* var r4 = StringUtils.replaceAll(is, "apple", "orange", "ig");
* var r5 = StringUtils.replaceAll(is, "apple", function (){
* return "orangeFn";
* }, "ig");
* var r6 = StringUtils.replaceAll("example with RegExp symbols like: - * [] / \\ $$", "$$", "dollarsymbols");
*
* logging.log("r1:" + r1) //logs: r1:an orange is an orange is an APPLE
* logging.log("r2:" + r2) //logs: r2:an orange is an orange is an APPLE
* logging.log("r3:" + r3) //logs: r3:an orange is an orange is an orange
* logging.log("r4:" + r4) //logs: r4:an orange is an orange is an orange
* logging.log("r5:" + r5) //logs: r5:an orangeFn is an orangeFn is an orangeFn
* logging.log("r6:" + r6) //logs: r6:example with RegExp symbols like: - * [] / \ dollarsymbols
*/
StringUtils.replaceAll = function(pPlainInputStr, pPlainSearchStr, pReplacement, pFlags)
{
//flags after creating the RegExp object are read only, so we have to determine the flags to bet set before creating the RegExp object
var regExpFlags;
if (pFlags)
if (pFlags.search("g") != -1)
regExpFlags = pFlags;
else
regExpFlags = "g" + pFlags;
else
regExpFlags = "g";
var searchRegExp = new RegExp(RegExpUtils.escapePatternStr(pPlainSearchStr), regExpFlags);
return pPlainInputStr.replace(searchRegExp, pReplacement);
};
/**
* concats severel elements by a separator; the separator is only applied if a element is not null and not an empty string "";
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment