Hi
I have below string VIN-RL-0001 , VIN-AK-002.
I want to extract RL ,AK from strings
Thanks
Hi
I have below string VIN-RL-0001 , VIN-AK-002.
I want to extract RL ,AK from strings
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit MohantyPosted May 16, 2023, 12:18 PM
Check this
Mohamed Azarudeen ZPosted May 16, 2023, 12:01 PM
To extract the substrings "RL" and "AK" from the given strings "VIN-RL-0001" and "VIN-AK-002," you can use regular expressions in JavaScript. Here's an example of how you can achieve this:
```javascript
const strings = ["VIN-RL-0001", "VIN-AK-002"];
const extractedStrings = strings.map((str) => {
const match = str.match(/VIN-(\w+)-/);
return match ? match[1] : null;
});
console.log(extractedStrings);
```
Output:
```
["RL", "AK"]
```
In the code above, we use the `match` method with a regular expression pattern `/VIN-(\w+)-/` to match the desired substring. The `(\w+)` part captures one or more word characters (letters, digits, or underscores) between "VIN-" and "-". The captured substring is then extracted using `match[1]`. The `map` function is used to apply this logic to each string in the `strings` array.
The resulting `extractedStrings` array will contain the extracted substrings "RL" and "AK" from the original strings.
Naimish MakwanaPosted May 15, 2023, 9:02 AM
Try below function:
Use like below:
Thanks
Tuhin PaulPosted May 15, 2023, 8:38 AM
Using regular expressions
Tuhin PaulPosted May 15, 2023, 8:37 AM
Using LINQ
Tuhin PaulPosted May 15, 2023, 8:36 AM
1. Using regular expressions
You can use regular expressions to extract the desired characters. The following regular expression will extract the two characters that come after the hyphen:
This regular expression will match any two consecutive word characters. In this case, the word characters are letters and numbers.
2. Using string slicing
You can also use string slicing to extract the desired characters. The following code will extract the two characters that come after the hyphen:
3. Using a for loopYou can also use a for loop to extract the desired characters. The following code will do this:
Jignesh KumarPosted May 14, 2023, 10:25 AM
Hello Ramco,
You can use split method and then you can skip first element.
out put : AK