如何在javascript中仅反转完整字符串中的intiger? [英] How do I reverse only intiger in full string in javascript?

查看:106
本文介绍了如何在javascript中仅反转完整字符串中的intiger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有

Input : "Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198"



需要得到这个 -


and need to get this -

Output : "Dlasdf432dkl sdfkl6888adaf51adfjk7 asdflkj166673a s544s891"





因为你看到只有数字被反转,我不知道如何从中隔离数字,并且只反转它们而不用触摸字母......可能很容易,但是我的js有点生锈,我找不到在线解决方案......我发现只有intiger但不能在一起..



我尝试过:





as u see only numbers are reversed, and im not sure how to isolate numbers from it, and reverse only them without toucnhing letters..probably easy,but my js is little bit rusty,i can't find solution online...i found for intiger only but not together..

What I have tried:

<script>
	function myfunction(a){
   var x=a.toString();
   var y= x.split(" ");
   var z=y.reverse();
   var result=z.join("");
     return result;
}

 myfunction( "Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198");


</script>

推荐答案

没有任何标准可以做到这一点。

你必须处理字符串,通过识别数字和非数字字符找到整数的开始和结束,然后交换结束:



There is nothing standard that will do that.
You will have to process the string, find the start and end of the integer by identifying digit and non-digit characters, then swap ends:

start:       123456
first swap:  623451
second swap: 653421
third swap:  654321


我的JavaScript非常......咳嗽,基本....但我喜欢到最后! :)



这是一个简单的HTML页面,毫无疑问,那些了解这些事情的人可以做得更好,但我希望这可以帮助你学到一些东西。 ..



My JavaScript is pretty ... ahem, basic.... but I enjoy getting there in the end! :)

Here's a simple HTML page that does it - no doubt those who know about these things ca do it far better, but I hope this helps you learn something...

<html>
  <head>
    <script type="text/javascript">
        function isNumeric(n) {
            return !isNaN(parseInt(n)) && isFinite(n);
        }
        function solveMe() {
            var sOut = ''   // string to hold final answer
            var sIn = document.getElementById("tIN").value;
            if (sIn.length > 0) {
                var n = [''];  // array to holder integers
                var a = [''];  // array to hold other characters
                var i = 0;
                var k; 
                var t;
                i = 0;
                while (i < sIn.length) {
                    if (isNumeric(sIn.charAt(i))) {
                         // if character at position i is numeric add it to array "n" at position i
                         // and a blank string to array "a" at position i
                        n.push(sIn.charAt(i));
                        a.push('');
                    } else {
                        // add to opposite arrays if it's not numeric
                        a.push(sIn.charAt(i));
                        n.push('');
                    }
                    i++;
                }
                i = 1;  // skip first eleemtn of array (it's ewmpty from initialisation))
                while (i < a.length) {
                    if (a[i] == '') {
                        // if element i in array "a" is empty, the correspsonding character in the "n" array will be a number
                        // so, first make a temp string of it and any successive numbers
                        k = i;
                        t = '';
                        while ((k < a.length) && (a[k] == '')) {
                            t += n[k];
                            k++;
                            i++;  // remember to increment i as well!
                        }
                        i--;  // and decrement by 1 at the end becasue we'll increment it again at end of while koop
                        // now add the characters in the temp string in reverse order to the answer string
                        k = t.length-1;
                        while (k > -1) {
                            sOut += t.charAt(k);
                            k--
                        }
                    } else {
                        // if element i in array "a" is a non-numeric character, simply add it to the answer string
                        sOut += a[i];
                    }
                    i++;
                }
            }
            document.getElementById("dX").innerHTML = sOut;
        }
    </script>
  </head>
  <body>
   <p><input type="text" id="tIN" style="width:500px" value="Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198" /></p>
      <p><a href="#" onclick="solveMe()">Solve</a></p>
      <div id="dX"></div>
  </body>
</html>


这是一个更短的版本 - 我不必要地绕过房子......

Here's a much shorter version - I was going round the houses unnecessarily above....
<html>
  <head>
    <script type="text/javascript">
        function isNumeric(n) {
            return !isNaN(parseInt(n)) && isFinite(n);
        }
        function solveMe() {
            var sOut = ''   // string to hold final answer
            var sIn = document.getElementById("tIN").value;
            if (sIn.length > 0) {
                var i = 0;
                var k; 
                var t;
                i = 0;
                while (i < sIn.length) {
                    if (isNumeric(sIn.charAt(i))) {
                        // if char is a number, first make a temp string of it and any successive numbers
                        k = i;
                        t = '';
                        while ((k < sIn.length) && (isNumeric(sIn.charAt(k)))) {
                            t += sIn.charAt(k);
                            k++;
                            i++;  // remember to increment i as well!
                        }
                        i--;  // and decrement by 1 at the end becasue we'll increment it again at end of while koop
                        // now add the characters in the temp string in reverse order to the answer string
                        k = t.length - 1;
                        while (k > -1) {
                            sOut += t.charAt(k);
                            k--
                        }                      
                    } else {
                        // add to opposite arrays if it's not numeric
                        sOut += sIn.charAt(i);
                    }
                    i++;
                }
            }
            document.getElementById("dX").innerHTML = sOut;
        }
    </script>
  </head>
  <body>
   <p><input type="text" id="tIN" style="width:500px" value="Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198" /></p>
      <p><a href="#" onclick="solveMe()">Solve</a></p>
      <div id="dX"></div>
  </body>
</html>


这篇关于如何在javascript中仅反转完整字符串中的intiger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆