Javascript检查字母是否为大写

时间:2018-04-19 13:25:15

标签: javascript uppercase

我正在将python代码转换为javascript,此代码的要点是检查是否使用大写字母并将其替换为连字符+同一字母的小写字母。

An unhandled exception has occurred in the function 'MoveNext'. Please see the trace log for more details. 

System.NullReferenceException: Object reference not set to an instance of an object. 
   at PX.Objects.IN.KitAssemblyEntry_Extension.report1(PXAdapter adapter) 
   at PX.Data.PXAction`1.a(PXAdapter A_0) 
   at PX.Data.PXAction`1.d__31.MoveNext() 
   at PX.Data.PXAction`1.d__31.MoveNext() 
   at PX.Data.PXAction`1.d__31.MoveNext() 
   at PX.Data.PXAction`1.d__31.MoveNext() 
   at PX.Web.UI.PXBaseDataSource.tryExecutePendingCommand(String viewName, String[] sortcolumns, Boolean[] descendings, Object[] searches, Object[] parameters, PXFilterRow[] filters, DataSourceSelectArguments arguments, Boolean& closeWindowRequired, Int32& adapterStartRow, Int32& adapterTotalRows) 
   at PX.Web.UI.PXBaseDataSource.ExecuteSelect(String viewName, DataSourceSelectArguments arguments, PXDSSelectArguments pxarguments) 
var string = "zIndex:5,background:transparent,borderRadius:0px,fontSize:8px";

String.prototype.isUpperCase = function() {
    return this.valueOf().toUpperCase() === this.valueOf();
};

function getIndices(string) {
    for (var s in string) {
        if (string[s].isUpperCase()) {
            string = string.replace(string[s], '-' + string[s].toLowerCase())
        }
    }
    return string
}

document.getElementById('default').innerHTML = string;
document.getElementById('tocss').innerHTML = getIndices(string);

问题在于,在检查字母时会阻塞标点符号。特殊字符无法理解如何处理它,我想知道是否有办法忽略CSS标点符号?

<div id="default"></div>
<br><hr><br>
<div id="tocss"></div>

3 个答案:

答案 0 :(得分:2)

您可以使用正则表达式并仅查找大写字母。然后用短划线和小写字母替换。

    <participant class="org.jpos.demo.group.Selector" name="Selector" logger="Q2">
            <property name="1100" value="Request" />
            <property name="1800" value="Echo" />
    </participant>

     <group name="Request">
            <participant class="org.jpos.demo.transaction.Request" logger="Q2" realm="prepare-response">
            </participant>
</group>

     <group name="Echo">
            <participant class="org.jpos.demo.transaction.Echo" logger="Q2" realm="prepare-echo">
            </participant>
         </group>

答案 1 :(得分:0)

基于@ MihaiAlexandru-lonut推荐,我放弃了扩展原型,使用此功能。

function isUpperCase(str) {
    return str == str.toUpperCase() && str != str.toLowerCase();
}

答案 2 :(得分:0)

您可以使用RegExp将所有大写字母替换为您想要的任何内容

&#13;
&#13;
var str = "zIndex:5,background:transparent,borderRadius:0px,fontSize:8px";

const regex = /[A-Z]+/g;


const newStr = str.replace(regex, (match) => `-${match}`.toLowerCase());

console.log('oldStr => ', str)
console.log('newStr => ', newStr)
&#13;
&#13;
&#13;