Im trying to split a user input value by the first space.
Example: User inputs the following command: /command test command into a input field.
The code now removes the first charackter in that string which would be: /
Now the code should split the remaining input ("command test command") by the first whitespace, so the following would remain:
- command
- test command
My problem is the following: My code splits the command at any whitespace. A example is made here:
$("#execute").click(function() {
var command = $("#command").val().substr(1);
var args = command.split(/[ ]+/);
var commandName = args[0];
args.shift()
$("#content").text("Command: " + commandName + " Text: " + args);
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Berechnen votes</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<input id="command" type="text" value="/command test command">
<button id="execute">Execute</button>
<br><br>
<span id="content"></span>
</body>
</html>
I appreciate any kind of help!