How to delete everything after first space In javascript

Hi good day

Anyone have idea to delete everything after first space in JavaScript .

Like i am extracting website.-- To get brand i have to use product title Like (samsung S6 ) i want only "samsung "

I tried all following code and many more with regex but its not working 

    var scrapedData = data;
    for (var brand in scrapedData){
        //var newbrand = brand.replace(/^[A-Za-z_]+/ ,'brand');
        //scrapedData[newbrand].brand_1  = (newbrand);
       // var newbrand = brand.replace ("brand" , "/^[A-Za-z_]+/")
       //scrapedData[brand].brand_1  = brand.replace ("brand" , "^[A-Za-z_]+")
  //brand.substr(0,brand.indexOf(' ')); 
  //scrapedData[brand].brand_1  = brand.replace(' ','<br />');
  
  scrapedData[brand].brand_1  =  brand.replace(/^(.*? ){1}/, brand);
   }
    data = scrapedData;

I am Getting data but like ,0,1,2,3,4  This number is in field //

Please help


You'd need to use split in this case, instead replace and index.

For example

var productname = scrapedData[i].ProductName;  // Samsung S6

var mySplitResult = myString.split(" "); // Split by space will result in ["Samsung", "S6"]
var brand = mySplitResult[0]; // Set the 0th array to brand variable

scrapedData[i].Brand = brand;

 


hi good day 
I tried this also, there is an error and its not working. please help me if is there anyway 

var scrapedData = data;
for (var brand in scrapedData){
    var productname = scrapedData[i].ProductName;
    var mySplitResult = myString.split(" ");
    var brand = mySplitResult[0];
    scrapedData[i].Brand = brand;
}
data = scrapedData;

 


What is the error? I just ran a test and it's working perfectly. here is my default scraper output

free data scraper software

Then I right clicked on output and then "Modify data using JavaScript" and pasted the same function.

modify the scraped data

See the documentation here to learn more and how to automatically apply the JS function on scraped data.


HI good day

 

Thank you for reply

I am getting following error

Please help

 

 

 


Looking on the screenshot seems you are just copy pasting the same JS function from here. You'd need to change the field name according to your data and analyze.

  • What is the field name and change in JS function (In my example it was PRODUCT_NAME)
  • What data this field have, is there space or not etc.

Or check our premium services where a data expert can create an agent for you

 


HI awesome thank you its working now thank you for reply

Can you please help me Once again in same Script .

I am getting all words after first space now but there is some problem some Fields is empty and some field contain number , i want to Set all blank and If they contain number  must be set as "unknown"

I got Some script is working but only If it got all number , is not working if is contain number and word . And if is blank , please help . i am using following script

 

var scrapedData = data;
for (var i in scrapedData){
var productname   = scrapedData[i].title;
var mysplitresult = productname.split(" "); 
var testing       = mysplitresult[0];
var result 
/*if (mysplitresult[0] =='2') result =(testing)
else result = ("Unknown");*/

if (isNaN(mysplitresult[0])) result =(testing)
    else result = ("Unknown") ;
   
scrapedData[i].brand  =  result;
 }
 data = scrapedData;

 

 

Your Answer

Close me