反转用户输入字符串

时间:2017-08-28 03:11:54

标签: javascript

一直在敲我的头。我试图通过表单接受用户输入并向后打印他们的句子。它不起作用,我不确定为什么。

我使用here中的一些代码作为反向字符串函数。

我尝试了很多不同的字符串反转函数,但是大多数都太复杂了,所以在这里相当初学者。 :)真的很感激任何帮助。



List<Integer> list = Arrays.asList(3, 4, 6, 1, 2, 3, 5, 6);  
list.parallelStream().forEach(System.out::print);        
list.parallelStream().map(a -> a + 2).forEach(a -> System.out.println(Thread.currentThread().getName() + "--" + a));
&#13;
function myFunction() {
    var sentence, text;
    sentence = document.getElementById("numb")
    sentence = sentence.replace( /^\s+|\s+$/g, " " )
    sentence = sentence.replace( /\s+/g, " " );
    
    var words = sentence.split(" ");a
        words = words.reverse();
        sentence = words.join(" ");
    		document.getElementById("demo").innerHTML = text;
}
&#13;
&#13;
&#13;

https://jsfiddle.net/7dscpebo/

5 个答案:

答案 0 :(得分:1)

这不是正则表达式的工作。只需在字符串上向后循环,然后逐个字符地创建一个新字符串。

var forward = 'this string is forward';

// create an empty string to fill with characters
var backward = '';

// loop backwards over forward
for (var i = forward.length - 1; i >= 0; i--) {
  backward += forward[i];
}

console.log(backward);

此外,您需要从sentence元素中获取文本值。

var forward = sentence.value;

答案 1 :(得分:1)

您的sentence对象是HTMLElement,而不是javascript字符串,因此您无法在其上调用字符串方法。我认为你的意思更像是

var sentence = document.getElementById("numb")

if (sentence) {
    //get the value of the Input element if it exists
    var sentenceText = sentence.value

    //...the rest of your code would go in here
}

答案 2 :(得分:1)

一个简单的单行翻转你的字符串:

&#13;
&#13;
var forward = "Forward sentence";

var backward = forward.split("").reverse().join("");

console.log(backward);
&#13;
&#13;
&#13;

将字符串拆分为其字符数组,反转数组,然后将其重新连接回字符串。从here获得了这个想法。

如果你只想颠倒句子中单词的顺序(而不是字符本身),那么只需.split().join(),空格如下:

&#13;
&#13;
var forward = "Forward sentence";

var backward = forward.split(" ").reverse().join(" ");

console.log(backward);
&#13;
&#13;
&#13;

我已将这些全部放在下面的原始示例中。一个修改是使用事件监听器,而不是在onclick中指定有时会产生this等问题的函数。

&#13;
&#13;
function reverseText() {
    var forward = document.getElementById("numb").value;
    var backward = forward.split("").reverse().join("");
    document.getElementById("demo").innerHTML = backward;
}

document.getElementById("btnsubmit").addEventListener("click", reverseText);
&#13;
<body>
  <p>Spell a sentence backwards.</p>
  <input id="numb">
  <button id="btnsubmit" type="button">Submit</button>
  <p id="demo"></p>
</body>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

请在修复代码中查看我的评论。

&#13;
&#13;
function myFunction() {
    var sentence; //text is not used
    sentence = document.getElementById("numb").value; 
    //Need to get the actual value, not just the DOM
    sentence = sentence.replace( /^\s+|\s+$/g, " " )
    sentence = sentence.replace( /\s+/g, " " );
    var words = sentence.split(" ");
        words = words.reverse();
        sentence = words.join(" ");
    		document.getElementById("demo").innerHTML = sentence;
}
&#13;
<body>
    <p >Spell a sentence backwards.</p>
    <input id="numb">
    <button type="button" onclick="myFunction()">Submit</button>
    <p id="demo"></p>
  </body>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

使用document.getElementById('numb').value获取输入框的,并使用数组的#reverse()方法将其反转:

value.split('').reverse().join('')

见下面的演示:

&#13;
&#13;
function myFunction() {
  var sentence = document.getElementById("numb").value;
  document.getElementById("demo").innerHTML = sentence.split('').reverse().join('');
}
&#13;
<body>
  <p>Spell a sentence backwards.</p>
  <input id="numb">
  <button type="button" onclick="myFunction()">Submit</button>
  <p id="demo"></p>
</body>
&#13;
&#13;
&#13;