Get the string after the character or another string in JavaScript

  • Use the split() method.
  • Refer to its second [1] element.
variable.split('character or string')[1];

Example:

<script>
   var field = 'Name: Anna';
   var name = field.split(': ')[1];

   console.log(name);
</script>

Output (console): Anna

Example 2:

<script>
   var sentence = 'My name is Kate';
   var onlyName = sentence.split('My name is ')[1];

   console.log(onlyName);
</script>

Output (console): Kate