这个示例 CKEditor javascript 函数有什么作用?
What does this sample CKEditor javascript function do?
MVC5
使用此 CKEditor documentation 中的信息,我最终能够从 MVC method/view 中集成图像选择过程,该过程显示可使用 CKEditor 插入文本区域的可用图像列表.
虽然最终的解决方案非常简单,但整个过程并不是特别直观。我最终会 post 我的解决方案,因为我确信许多相对较新的 MVC 编码人员(例如我自己)正在为此功能寻找简单直接的解决方案。但与此同时,
下面的代码显示了上面 link 中 示例 2 的相关行,稍微重新排列。
<body>
<button onclick="returnFileUrl()">Select File</button>
</body>
<script>
function getUrlParam( paramName ) { // Helper function to get parameters from the query string.
var reParam = new RegExp( '(?:[\?&]|&)' + paramName + '=([^&]+)', 'i' );
var match = window.location.search.match( reParam );
return ( match && match.length > 1 ) ? match[1] : null;
}
function returnFileUrl() { // Simulate user action of selecting a file to be returned to CKEditor
var funcNum = getUrlParam( 'CKEditorFuncNum' );
var fileUrl = '/path/to/file.txt';
window.opener.CKEDITOR.tools.callFunction( funcNum, fileUrl );
window.close();
}
</script>
我一直不知道如何使用 getUrlParam()。我最终只是绕过了它,并通过变量 funcNum 提供了已传递到我的图像选择器方法中的确切参数。一旦我这样做了,来自 CKEditor 的示例代码工作得很好。
但是 getUrlParam() 函数在做什么?我只是名义上理解 RegExp,而这个完全让我不知所措。任何人都可以解释为什么甚至建议它吗?
getUrlParam
接受一个 paramName
(如 http://google.com/search.php?q=term
中的 q
),定义一个匹配 param
并捕获的 RegExp
该方法返回的第 1 组(match[1]
,例如 term
)中的值。 window.location.search
获取 JS 中当前 window URL 的查询字符串部分(例如 ?q=term
)。
我会用更简单的
替换正则表达式定义
var reParam = new RegExp( '[?&]' + paramName + '=([^&]+)', 'i');
生成的正则表达式看起来像 [?&]q=([^&]+)
匹配:
[?&]
- ?
或 &
(在您的原始代码中,它是 (?:[\?&]|&)
,匹配 ?
或 &
,或 &
- 因此,我建议缩短)
q=
- 字符的文字序列 q=
([^&]+)
- 第 1 组捕获 &
. 以外的一个或多个字符
在 VB.NET 中,您可以使用如下内容:
Private Shared Function getUrlParam(paramName As String) As String
Dim reParam = "(?i)[?&]" & Regex.Escape(paramName) & "=([^&]+)"
Dim match As Match = Regex.Match("http://google.com/index.php?q=term", reParam)
If match.Success = True And match.Value.Length > 1 Then
Return match.Groups(1).Value
Else
Return String.Empty
End If
End Function
然后用 Dim res As String = getUrlParam("q")
.
调用
MVC5
使用此 CKEditor documentation 中的信息,我最终能够从 MVC method/view 中集成图像选择过程,该过程显示可使用 CKEditor 插入文本区域的可用图像列表.
虽然最终的解决方案非常简单,但整个过程并不是特别直观。我最终会 post 我的解决方案,因为我确信许多相对较新的 MVC 编码人员(例如我自己)正在为此功能寻找简单直接的解决方案。但与此同时,
下面的代码显示了上面 link 中 示例 2 的相关行,稍微重新排列。
<body>
<button onclick="returnFileUrl()">Select File</button>
</body>
<script>
function getUrlParam( paramName ) { // Helper function to get parameters from the query string.
var reParam = new RegExp( '(?:[\?&]|&)' + paramName + '=([^&]+)', 'i' );
var match = window.location.search.match( reParam );
return ( match && match.length > 1 ) ? match[1] : null;
}
function returnFileUrl() { // Simulate user action of selecting a file to be returned to CKEditor
var funcNum = getUrlParam( 'CKEditorFuncNum' );
var fileUrl = '/path/to/file.txt';
window.opener.CKEDITOR.tools.callFunction( funcNum, fileUrl );
window.close();
}
</script>
我一直不知道如何使用 getUrlParam()。我最终只是绕过了它,并通过变量 funcNum 提供了已传递到我的图像选择器方法中的确切参数。一旦我这样做了,来自 CKEditor 的示例代码工作得很好。
但是 getUrlParam() 函数在做什么?我只是名义上理解 RegExp,而这个完全让我不知所措。任何人都可以解释为什么甚至建议它吗?
getUrlParam
接受一个 paramName
(如 http://google.com/search.php?q=term
中的 q
),定义一个匹配 param
并捕获的 RegExp
该方法返回的第 1 组(match[1]
,例如 term
)中的值。 window.location.search
获取 JS 中当前 window URL 的查询字符串部分(例如 ?q=term
)。
我会用更简单的
替换正则表达式定义var reParam = new RegExp( '[?&]' + paramName + '=([^&]+)', 'i');
生成的正则表达式看起来像 [?&]q=([^&]+)
匹配:
[?&]
-?
或&
(在您的原始代码中,它是(?:[\?&]|&)
,匹配?
或&
,或&
- 因此,我建议缩短)q=
- 字符的文字序列q=
([^&]+)
- 第 1 组捕获&
. 以外的一个或多个字符
在 VB.NET 中,您可以使用如下内容:
Private Shared Function getUrlParam(paramName As String) As String
Dim reParam = "(?i)[?&]" & Regex.Escape(paramName) & "=([^&]+)"
Dim match As Match = Regex.Match("http://google.com/index.php?q=term", reParam)
If match.Success = True And match.Value.Length > 1 Then
Return match.Groups(1).Value
Else
Return String.Empty
End If
End Function
然后用 Dim res As String = getUrlParam("q")
.