Javascript str.replace与美元符号不工作

时间:2016-02-29 10:11:52

标签: javascript jquery

要替换的示例字符串:

https://fw.adsafeprotected.com/rjss/bs.serving-sys.com/52023/7720220/BurstingPipe/adServer.bs?cn=rsb&c=28&pli=1234567890&PluID=0&w=300&h=600&ord=[timestamp]&ucm=true&ncu=$${CLICK_URL_ENC}$&adsafe_preview=${IS_PREVIEW}`

我正在努力做出的替换:

$${CLICK_URL_ENC}$  --> $$${CLICK_URL_ENC}$$
[timestamp]         --> ${CACHEBUSTER}

期望的输出:

https://fw.adsafeprotected.com/rjss/bs.serving-sys.com/52023/7720220/BurstingPipe/adServer.bs?cn=rsb&c=28&pli=1234567890&PluID=0&w=300&h=600&ord=${CACHEBUSTER}&ucm=true&ncu=$$${CLICK_URL_ENC}$$&adsafe_preview=${IS_PREVIEW}

代码我尝试过:

代码:

var v = $("textarea#creative-content").val();
v = v.replace(/\$\$\{CLICK\_URL\_ENC\}\$/g, "$$${CLICK_URL_ENC}$$");
v = v.replace("[timestamp]","${CACHEBUSTER}");
console.log(v);

输出:

  • [timestamp]更改为${CACHEBUSTER}
  • $${CLICK_URL_ENC}$更改为$$${CLICK_URL_ENC}$$

代码:

var v = $("textarea#creative-content").val();
v = v.replace("$${CLICK_URL_ENC}$", "$$${CLICK_URL_ENC}$$");
v = v.replace("[timestamp]","${CACHEBUSTER}");
console.log(v);

输出:

  • [timestamp]更改为${CACHEBUSTER}
  • $${CLICK_URL_ENC}$更改为$$${CLICK_URL_ENC}$$

代码:

var v = $("textarea#creative-content").val();
v = v.replace("\$\${CLICK_URL_ENC}\$", "\$\$\${CLICK_URL_ENC}\$\$");
v = v.replace("[timestamp]","${CACHEBUSTER}");
console.log(v);

输出:

  • [timestamp]更改为${CACHEBUSTER}
  • $${CLICK_URL_ENC}$更改为$$${CLICK_URL_ENC}$$

如何使用JavaScript / jQuery进行我正在寻找的更改?

2 个答案:

答案 0 :(得分:1)

您需要 GUI Window tries to begin rendering while something else has not finished rendering! Either you have a recursive OnGUI rendering, or previous OnGUI did not clean up properly. InvalidOperationException: Operation is not valid due to the current state of the object System.Collections.Stack.Peek () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections/Stack.cs:321) UnityEngine.GUILayoutUtility.EndLayoutGroup () (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:280) UnityEditor.DockArea.EndOffsetArea () (at C:/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:425) UnityEditor.DockArea.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:348) ArgumentException: Getting control 1's position in a group with only 1 controls when doing Repaint Aborting UnityEngine.GUILayoutGroup.GetNext () (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:656) UnityEngine.GUILayoutUtility.BeginLayoutArea (UnityEngine.GUIStyle style, System.Type layoutType) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:297) UnityEngine.GUILayoutUtility.DoBeginLayoutArea (UnityEngine.GUIStyle style, System.Type layoutType) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:312) UnityEditor.EditorGUILayoutUtilityInternal.BeginLayoutArea (UnityEngine.GUIStyle style, System.Type LayoutType) (at C:/buildslave/unity/build/Editor/Mono/GUI/EditorGUIInternal.cs:65) UnityEditor.DockArea.BeginOffsetArea (Rect screenRect, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:431) UnityEditor.DockArea.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:323) device.IsInsideFrame() 美元符号。这是通过连续输入两个来完成的。你想在开头有3个美元符号,这意味着你必须在替换字符串中有6个美元符号:

escape

答案 1 :(得分:1)

在替换中$是一个特殊的字符,当你捕获一个组时,它需要被转义...带有$符号,这给了我们一些不可读的东西,如:

for i in xrange(10):
    #loop body
    pass
else:
    #do this when loop is completed without any break
    pass