Skip to content
Advertisement

How to split a readLine but dont split values inside apostrophes?

Example txt file

JavaScript

Code

JavaScript

Is there a way I can have the split not split items within the apostrophes when going across the line? That way regardless if the first Item is one word or two words, if I call inputWords[1] It will always return the full string.

What happens: “Multi Bit Ratcheting” -> inputWords[1] -> ‘Multi

What I want: “Multi Bit Ratcheting” -> inputWords[1] -> ‘Multi Bit Ratcheting’

Advertisement

Answer

You could apply a regex find all to each line using the pattern '.*?'|S+:

JavaScript

You may apply the above logic to each line from the file. You should, however, define the pattern outside the loop so that it doesn’t have to be recompiled for every line.

Your updated code:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement