11931

JavaScript - strip everything before and including a character

Question:

I am relatively new to RegEx and am trying to achieve something which I think may be quite simple for someone more experienced than I.

I would like to construct a snippet in JavaScript which will take an input and strip anything before and including a specific character - in this case, an underscore.

Thus 0_test, 1_anotherTest, 2_someOtherTest would become test, anotherTest and someOtherTest, respectively.

Thanks in advance!

Answer1:

You can use the <a href="https://regex101.com/r/sV2jN7/2" rel="nofollow">following regex</a> (<em>which can only be great if your special character is not known</em>, see Alex's solution for just _):

^[^_]*_

Explanation:

<ul><li>^ - Beginning of a string</li> <li>[^_]* - Any number of characters other than _</li> <li>_ - Underscore</li> </ul>

And replace with empty string.

<pre class="snippet-code-js lang-js prettyprint-override">var re = /^[^_]*_/; var str = '1_anotherTest'; var subst = ''; document.getElementById("res").innerHTML = result = str.replace(re, subst); <pre class="snippet-code-html lang-html prettyprint-override"><div id="res"/>

If you have to match before a digit, and you do not know which digit it can be, then the regex way is better (with the /^[^0-9]*[0-9]/ or /^\D*\d/ regex).

Answer2:

Simply read from its position to the end:

var str = "2_someOtherTest"; var res = str.substr(str.indexOf('_') + 1);

Recommend

  • Can C++ compiler assume a const bool & value will not change?
  • How can I do test setup using the testing package in Go
  • MySQL: Getting highest score for a user
  • Neo4j, get all relationships between a set of nodes
  • Setting lazy static variable first initializes then assigns?
  • wxwidgets and threads
  • Get all keys of a nested dictionary [duplicate]
  • Jax-RS and Xmlhttp Communication
  • Objective C: when valueForKey is an instance with properties
  • How can I insert a snippet on a new line with vscode?
  • Calling exit() in C++ library terminates python script that wrapps that library using swig
  • Advantages of pass-by-value and std::move over pass-by-reference
  • JavaScript - strip everything before and including a character
  • Is filter: progid:DXImageTransform.Microsoft.DropShadow(OffX=“x”, OffY=“y”, Color=“color”) a viable
  • How to let C# designer edit my struct property?
  • OSX 10.7.5: fatal error: 'ft2build.h' file not found
  • How to add a field to a dbf file?
  • push into deeply nested array
  • Bluetooth debugging on android wear failing
  • mysql: select query when the column that has condition on has NULL value
  • remotely and programatically logoff an active domain user
  • Git command “add -p” on all files
  • How to iterate registry from the root?
  • Assign a firebase database url(String) to a Database Reference variable?
  • Using VSTS Rest API, how do you Update a Markdown widget?
  • Should I use redundancy or a simple query on a large dataset with Firebase Cloud Firestore database?
  • django filer - ImportError: cannot import name mixins
  • How to create mentions for names like “@myname” using javascript?
  • Jquery hide first 12 elementes, show next 12 elements
  • How to re-enable Test::Unit in Rails 4 after 'rails new app --skip-test-unit'
  • How can I do some cleanup right before an Android app is quitting?
  • Python urlparse: small issue
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • R: gsub and capture
  • Benchmarking RAM performance - UWP and C#
  • Net Present Value in Excel for Grouped Recurring CF