rJ7 rJ7 - 1 month ago 5
HTML Question

How to get a particular word from the sentence?

I have input value like below:

<input type="text" value: "Part: PT001 Invoice: 1234" />


what I need is I want to get the part number alone "PT001" from the value of the input..

Note: PT001 is dynamically getting value...

Appreciate your thoughts...

Answer

Use String#match method with regex /\bPart:\s?(\S+)/ and get the capturing group value.

console.log(
  $('input').val().match(/\bPart:\s?(\S+)/)[1]
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="Part: PT001 Invoice: 1234" />

Comments